Ad

Bootstrap Tooltip Dynamic Value Dependant On A Filled Out Required Or Not

- 1 answer

Hi I'm running my head in to a wall.

I am trying to make a javascript/jquery event that detects when my mouse is over a button. when the mouse is over the button I want a tooltip containing the required message from the input that still needs to be filled out. I want it to only be the last required message and I want the message to change until there are no more required to fill out.

This is what I have tried so far.

my Razor cshtml

...Irrelevant html code
 <div class="form-group form-group-sm col-sm-6">
                    <div class="row">
                        <div class="col-sm-1"></div>
                        <div class="col-sm-4">
                            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "col-form-label col-md-12" })
                        </div>

                        <div class="col-sm-6">
                            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control",title = Global.ToolTipName, data_toggle = "tooltip" } })
                            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>
                  <div class="form-group form-group-sm col-sm-6">
                    <div class="row">
                        <div class="col-sm-1"></div>
                        <div class="col-sm-4">
                            @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "col-form-label col-md-12" })
                        </div>
                        <div class="col-sm-6">
                            @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control", title = Global.ToolTipPhone, data_toggle = "tooltip" } })
                            @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>
                 <div class="form-group form-group-sm col-sm-12">
                    <div class="row">
                        <div class="col-sm-12">
                            <input id="Send" type="submit" value="@Global.btnSend" title = "Not Implemented Yet!" data_toggle = "tooltip" class="btn btn-default" />
                        </div>
                    </div>
                </div>

...Irrelevant html code

and my script looks like this

   <script type="text/javascript">
   $(document).on('mouseenter', '#Send', function () {
           var allinput = $('input[data-val-required]').get();
           allinput.forEach(item => this.attr('data-original-title', item.title));//<--- tried this doesn't seem to work.
           //allinput.forEach(item => this.attr('data-original-title', item.title).tooltip('fixTitle').tooltip('show')); <--- and tried this didn't work either
           //allinput.forEach(item => this.attr('title', item.title).tooltip('fixTitle').tooltip('show'));<--- then I tried this didn't work either
           $(this).tooltip('show');
        });

        $(document).on('mouseleave', '#Send', function () {
            $(this).tooltip('hide');
        });
        </script>

So I am at a loss where I went wrong here. a pointer to the right direction or a little help here would be appreciated, Even telling me if I am Far off my mark or very close to would also be considered a big help.

with the help of CodeThing I've produced a working example of what I wanted to do.

 $(document).on('mouseenter', '#Send', function () {
            var allinput = $('input[data-val-required]').get();
            var lastval = '';
            $.each(allinput, function () {
                if ($(this).val() == '') {
                    var forName = this.id;
                    var closestName = "label[for='" + forName + "']";
                    lastval += $(this.parentNode.parentNode).find(closestName).text() +", ";
                }
            });
            lastval = "Need to fill out these fields: " + lastval
            $(this).attr('title', lastval).tooltip('fixTitle').tooltip('show');
        });

        $(document).on('mouseleave', '#Send', function () {
            $(this).tooltip('hide');
        });

However this only works in Firefox. I really need this also to work in both Edge and Chrome.

Ad

Answer

In that case, try the below code and add data-html="true" attribute to send button. It display all blank fields tooltip on new line in send buttons tooltip.

$(document).on('mouseenter', '#Send', function () {
    var allinput = $('input[data-val-required]').get();
    var lastval = '';
    $.each(allinput, function() {
        if($(this).val() == '') lastval += $(this).attr('title')+'<br />';
    });
    $(this).attr('title', lastval).tooltip('fixTitle').tooltip('show');
});

$(document).on('mouseleave', '#Send', function () {
    $(this).tooltip('hide');
}); 
Ad
source: stackoverflow.com
Ad