Change Twitter Bootstrap Tooltip content on click

前端 未结 25 1021
情深已故
情深已故 2020-11-27 10:02

I have a tooltip on an anchor element, that sends an AJAX request on click. This element has a tooltip (from Twitter Bootstrap). I want the tooltip content to change when th

相关标签:
25条回答
  • 2020-11-27 10:40

    In bootstrap 4 I just use $(el).tooltip('dispose'); then recreate as normal. So you can put the dispose before a function that creates a tooltip to be sure it doesn't double up.

    Having to check the state and tinker with values seems less friendly.

    0 讨论(0)
  • 2020-11-27 10:42

    Just found this today whilst reading the source code. So $.tooltip(string) calls any function within the Tooltip class. And if you look at Tooltip.fixTitle, it fetches the data-original-title attribute and replaces the title value with it.

    So we simply do:

    $(element).tooltip('hide')
              .attr('data-original-title', newValue)
              .tooltip('fixTitle')
              .tooltip('show');
    

    and sure enough, it updates the title, which is the value inside the tooltip.

    A shorter way:

    $(element).attr('title', 'NEW_TITLE')
              .tooltip('fixTitle')
              .tooltip('show');
    
    0 讨论(0)
  • 2020-11-27 10:42

    Here is update for the Bootstrap 4:

    var title = "Foo";
    elt.attr('data-original-title', title);
    elt.tooltip('update');
    elt.tooltip('show');
    

    But the best way is to do like this:

    var title = "Foo";
    elt.attr('title', title);
    elt.attr('data-original-title', title);
    elt.tooltip('update');
    elt.tooltip('show');
    

    or inline:

    var title = "Foo";
    elt.attr('title', title).attr('data-original-title', title).tooltip('update').tooltip('show');
    

    From the UX side you just see that text is changed with no fading or hide/show effects and there is no needs for the _fixTitle.

    0 讨论(0)
  • 2020-11-27 10:42

    You can just change the data-original-title using the following code:

    $(element).attr('data-original-title', newValue);
    
    0 讨论(0)
  • 2020-11-27 10:42

    Thanks this code was very helpful for me, i found it effective on my projects

    $(element).attr('title', 'message').tooltip('fixTitle').tooltip('show');

    0 讨论(0)
  • 2020-11-27 10:44

    With Tooltip object Boostrap :

    $(element).attr('data-original-title', 'New value');
    $(element).data('bs.tooltip').tip().find('.tooltip-inner').text('New value');
    
    0 讨论(0)
提交回复
热议问题