Change Twitter Bootstrap Tooltip content on click
Solution 1
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');
Solution 2
In Bootstrap 3 it is sufficient to call elt.attr('data-original-title', "Foo")
as changes in the "data-original-title"
attribute already trigger changes in the tooltip display.
UPDATE: You can add .tooltip('show') to show the changes immediately, you need not to mouseout and mouseover target to see the change in the title
elt.attr('data-original-title', "Foo").tooltip('show');
Solution 3
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
.
Solution 4
you can update the tooltip text without actually calling show/hide:
$(myEl)
.attr('title', newTitle)
.tooltip('fixTitle')
.tooltip('setContent')
Solution 5
for Bootstrap 4:
$(element).attr("title", "Copied!").tooltip("_fixTitle").tooltip("show").attr("title", "Copy to clipboard").tooltip("_fixTitle");
Related videos on Youtube
![rebellion](https://i.stack.imgur.com/sGHuB.jpg?s=256&g=1)
rebellion
Updated on November 11, 2021Comments
-
rebellion over 2 years
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 the AJAX request returns successfully. How can I manipulate the tooltip after initiation?
-
James McMahon about 11 yearsYou can also change the title through the data('tooltip').options property, see stackoverflow.com/questions/10181847/…
-
Daniel Miliński about 11 yearsBest Answer to this.Question.
-
ajbeaven over 10 yearsWith bootstrap 3, it becomes
$('#tooltip_id').data('bs.tooltip').options.title = 'New_value!';
-
ndreckshage over 10 yearsscroll down (or stackoverflow.com/a/20713610/1350306) to change text without closing/reopening the toolip.
-
Nigel Angel about 10 yearsIf you're using boostrap3 you need to use
data('bs.tooltip')
instead ofdata('tooltip')
-
Augustin Riedinger over 9 yearsSays
$tip
is not an attribute of$(element)
-
Augustin Riedinger over 9 yearsDid that:
$(element).attr('title', newTitle).tooltip('fixTitle').parent().find('.tooltip .tooltip-inner').text(newTitle);
-
user2846569 over 9 yearsCouldn't find documentation on 'fixTitle', so I guess not a good idea to use this!? ...
-
tremby about 9 yearsI'm updating a tooltip on a neighbouring element when typing into a text field, and the tooltip is disappearing for me with this code when I press a key.
-
Wojciech Zylinski about 9 yearsThanks for this. The only problem with this solution is that it seems to mess up tooltip position after text change.
-
markau about 9 yearsThis, with
.tooltip('show')
on the end of the chain, worked for me. -
thouliha almost 9 yearsHey relational, could you change your answer to reflect the comments corrections? Thanks.
-
Roy Hyunjin Han almost 9 yearsYou can safely omit the call to fixTitle: $(element).attr('data-original-title', 'xyz')
-
brauliobo almost 9 yearsyeah,
setContent
andshow
does the trick! you can changedata-original-title
directly instead of usingfixTitle
-
Lev Lukomsky almost 9 yearsIt not applies changes immediately, you need to mouseout and mouseover target to see the new title text
-
Jared over 8 yearsThis is actually the best answer and you can add
.tooltip('show');
to get it to show after the update -
molerat over 8 yearsIs there a way to update the text without manipulating the title-attribute?
-
sksallaj over 8 yearsI removed tooltip('hide') and tooltip('fixTitle'), and it worked still
-
Gabe O'Leary over 8 yearsThis actually didn't work for me, the tooltip switched values and then disappeared quickly.
$(element).attr('data-original-title', newValue).tooltip('show');
was the simplest working solution for me. -
jyoseph about 8 yearsThis is the only solution (out of many tried) that seemed to just work (bs3). Nice solution for sure!
-
jac1013 over 7 yearsI was having trouble with the tooltip after using both methods (it was disappearing a not coming back again), it was basically because of the native focus so adding
$element.trigger('focusout')
fixed that problem for me. -
Geoffroy CALA over 7 yearsIn my case @lukmdo solution didn't work properly. I still could see the old title for a moment so the
tooltip('hide')
was useful. -
Ayyaz Zafar about 7 yearsThis solution is better than all others
-
César León almost 7 yearsThis works in firefox, chrome, IE and Edge, "$(element).attr('data-original-title', newValue).tooltip('show');" , was indeed the simplest working solution, thanks.
-
Jamie M about 6 years@fsasvari in bootstrap 4 just change it to '_fixTitle'
-
Mubramaj almost 6 yearsI am using Bootstrap v4.1.2 and I had to use _fixTitle instead of fixTitle
-
Fifi almost 6 yearsShould we call $(el).tooltip('dispose') before or after updating the tooltip title ?
-
Martin Lyne almost 6 years@imabot dispose of old tooltip, then recreate with new data. If your tooltips are complex you may want to retrieve or otherwise store the initial config so you can just recreate with that config + new values. Hope that makes sense.
-
Chloe almost 6 yearsThis uses undocumented API. Beware.
-
Ricardo about 5 yearsTested on Bootstrap v2.3.1
-
Hammad Sajid about 5 yearswhy it didn't work when i use single quote as in second parameter around
'Foo'
like.attr('data-original-title', 'Foo')
?? -
Ben in CA over 4 yearsBootstrap 4 - noted below - use .tooltip('update');
-
Fouad about 4 years$(element).attr('title', 'NEW_TITLE') is enough to update the title
-
Yes Barry almost 4 yearsThis feels like the cleanest solution.
-
Alexander over 2 yearsThe solution worked for me, thanks! In general, please note that you can only update tooltips which are enabled. So in case you have manually disabled a tooltip, you have to re-enable it by using elt.tooltip('enable') before updating it.
-
JV conseil about 2 yearsSince Bootstrap v5 it is
data-bs-original-title
instead ofdata-original-title
-
Vikcen about 2 yearsIn my case works without ".tooltip('fixTitle')" just in this way: $(element).tooltip('hide') .attr('data-original-title', newValue) .tooltip('show');