The need is to have a series of icons on a page which toggles their behavior when clicked. Each action icon has a tooltip which reflects the action to be taken in each of the two states.
here is an example of an action icon.
<span name="no_quote_action" class="btn btn-xs btn-action " style="border: none"
rel="tooltip" data-placement="right" data-trigger="hover"
title="No-Quote this line" onclick="setNoQuote(this)">
<span class="glyphicon glyphicon-remove" style="color: #ff0000"></span>
</span>
On the page there is a script tag which outlines the two actions.
<script>
function setNoQuote(_this) {
var button = $(_this);
button.children().attr('class', 'glyphicon glyphicon-plus');
button.children().css('color', '#00ff00');
button.attr('onclick', 'setDoQuote(this)');
button.tooltip('destroy');
// button.attr('data-original-title', 'Undo No-Quote');
button.attr('title', 'Undo No-Quote');
setTips();
}
function setDoQuote(_this) {
var button = $(_this);
button.children().attr('class', 'glyphicon glyphicon-remove');
button.children().css('color', '#ff0000');
button.attr('onclick', 'setNoQuote(this)');
button.tooltip('destroy');
// button.attr('data-original-title', 'No-Quote this line');
button.attr('title', 'No-Quote this line');
setTips();
}
function setTips() {
$('[rel="tooltip"]').tooltip({html: true});
}
$(window).load(function () {
setTips();
});
The problem I'm seeing is that, while the icon changes, and the previous tooltip is destroyed, the setTips();
call does not successfully add the new tooltip to the icon that was clicked. It seems like a timing issue or something because calling setTips()
manually after the event completes works as expected.
Below is a complete HTML example page which demonstrates the issue.
Also of note, clicking in rapid succession will give different results.
Please help me understand what is going on and why. What best practice should I be using to avoid such issues?