Change Twitter Bootstrap Tooltip content on click

前端 未结 25 979
情深已故
情深已故 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:25

    heres a nice solution if you want to change the text without closing and reopening the tooltip.

    $(element).attr('title', newTitle)
              .tooltip('fixTitle')
              .data('bs.tooltip')
              .$tip.find('.tooltip-inner')
              .text(newTitle)
    

    this way, text replaced without closing tooltip (doesnt reposition, but if you are doing a one word change etc it should be fine). and when you hover off + back on tooltip, it is still updated.

    **this is bootstrap 3, for 2 you probably have to change data/class names

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

    For bootstrap 3.x

    This seems like cleanest solution:

    $(element)
      .attr('data-original-title', 'New title').tooltip('show')
    

    Show is used to make title update right away and not wait for tooltip to be hidden and shown again.

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

    The following worked the best for me, basically I'm scrapping any existing tooltip and not bothering to show the new tooltip. If calling show on the tooltip like in other answers, it pops up even if the cursor isn't hovering above it.

    The reason I went for this solution is that the other solutions, re-using the existing tooltip, led to some strange issues with the tooltip sometimes not showing when hovering the cursor above the element.

    function updateTooltip(element, tooltip) {
        if (element.data('tooltip') != null) {
            element.tooltip('hide');
            element.removeData('tooltip');
        }
        element.tooltip({
            title: tooltip
        });
    }
    
    0 讨论(0)
  • 2020-11-27 10:26

    Destroy any pre-existing tooltip so we can repopulate with new tooltip content

    $(element).tooltip("destroy");    
    $(element).tooltip({
        title: message
    });
    
    0 讨论(0)
  • 2020-11-27 10:26

    Change the text by altering the text in the element directly. (does not update the tooltip position).

    $('.tooltip-inner', $element.next()).html(newHtml);
    $('.tooltip-inner', $element.next()).text(newText);
    

    Change the text by destroying the old tooltip, then creating and showing a new one. (Causes the old one to fade out and the new one to fade in)

    $element
    .tooltip('destroy')
    .tooltip({
        // Repeat previous options.
        title: newText,
    })
    .tooltip('show');
    

    I'm using the top method to both animate the "Saving." message (using &nbsp so the tooltip does not change in size) and to change the text to "Done." (plus padding) when the request completes.

    $element.tooltip({
      placement: 'left',
      title: 'Saving...',
      trigger: 'manual',
    }).tooltip('show');
    
    var parent = $element.parent();
    var interval_id = setInterval(function(){
        var text = $('.tooltip-inner', parent).html();
        switch(text) {
        case 'Saving.  ': text = 'Saving.. '; break;
        case 'Saving.. ': text = 'Saving...'; break;
        case 'Saving...': text = 'Saving.  '; break;
        }
        $('.tooltip-inner', parent).html(text);
    }, 250);
    
    send_request( function(){
        // When the request is complete
        clearInterval(interval_id);
        $('.tooltip-inner', parent).html('Done.    ');
        setTimeout(function() {
            $element.tooltip('hide');
        }, 1500 /* Show "Done." for a bit */);
    });
    
    0 讨论(0)
  • 2020-11-27 10:28

    I'm using this easy way out:

    $(document).ready(function() {
        $("#btn").prop('title', 'Click to copy your shorturl');
    });
    
    function myFunction(){
      $(btn).tooltip('hide');
      $(btn).attr('data-original-title', 'Test');
      $(btn).tooltip('show');
    });

    0 讨论(0)
提交回复
热议问题