Skip to content

Commit dee7c8b

Browse files
committed
Tooltip: Update open tooltips if the content option changes. Fixes #8544 - Unable to update tooltip content dynamically.
1 parent c135fc1 commit dee7c8b

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

tests/unit/tooltip/tooltip_options.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ asyncTest( "content: sync + async callback", function() {
4646
}).tooltip( "open" );
4747
});
4848

49+
test( "content: change while open", function() {
50+
expect( 2 ) ;
51+
var element = $( "#tooltipped1" ).tooltip({
52+
content: function() {
53+
return "old";
54+
}
55+
});
56+
57+
element.one( "tooltipopen", function( event, ui ) {
58+
equal( ui.tooltip.text(), "old", "original content" );
59+
element.tooltip( "option", "content", function() {
60+
return "new";
61+
});
62+
equal( ui.tooltip.text(), "new", "updated content" );
63+
});
64+
65+
element.tooltip( "open" );
66+
});
67+
4968
test( "items", function() {
5069
expect( 2 );
5170
var event,

ui/jquery.ui.tooltip.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ $.widget( "ui.tooltip", {
7373
},
7474

7575
_setOption: function( key, value ) {
76+
var that = this;
77+
7678
if ( key === "disabled" ) {
7779
this[ value ? "_disable" : "_enable" ]();
7880
this.options[ key ] = value;
7981
// disable element style changes
8082
return;
8183
}
84+
8285
this._super( key, value );
86+
87+
if ( key === "content" ) {
88+
$.each( this.tooltips, function( id, element ) {
89+
that._updateContent( element );
90+
});
91+
}
8392
},
8493

8594
_disable: function() {
@@ -114,9 +123,7 @@ $.widget( "ui.tooltip", {
114123
},
115124

116125
open: function( event ) {
117-
var content,
118-
that = this,
119-
target = $( event ? event.target : this.element )
126+
var target = $( event ? event.target : this.element )
120127
.closest( this.options.items );
121128

122129
// No element to show a tooltip for
@@ -140,19 +147,26 @@ $.widget( "ui.tooltip", {
140147

141148
target.data( "tooltip-open", true );
142149

150+
this._updateContent( target, event );
151+
},
152+
153+
_updateContent: function( target, event ) {
154+
var content,
155+
that = this;
156+
143157
content = this.options.content.call( target[0], function( response ) {
144158
// ignore async response if tooltip was closed already
145159
if ( !target.data( "tooltip-open" ) ) {
146160
return;
147161
}
148162
// IE may instantly serve a cached response for ajax requests
149163
// delay this call to _open so the other call to _open runs first
150-
setTimeout(function() {
151-
that._open( event, target, response );
152-
}, 1 );
164+
that._delay(function() {
165+
this._open( event, target, response );
166+
});
153167
});
154168
if ( content ) {
155-
that._open( event, target, content );
169+
this._open( event, target, content );
156170
}
157171
},
158172

0 commit comments

Comments
 (0)