Hillary Sanders Hillary Sanders - 24 days ago 13
Javascript Question

Changing data-tooltip with AJAX isn't working

I have a button in my html template:

<a class="tooltipped" id="stash_recipe_tooltip" data-position="bottom" data-delay="50"
data-tooltip="{{ stash_tooltip }}">
<div id="stash_recipe_btn" class="detail_footer_btn btn-floating col2 center"> + </div>
</a>


later, in the html file, I have:

<script>
function add_recipe_to_stash() {
var stash_plus_or_minus = document.getElementById("stash_recipe_btn").innerHTML
$.ajax({
url: '/ajax/add_recipe_to_stash/',
type: 'GET',
data: {
# this goes to a django view that, in essence, returns a new 'stash_tooltip' var and 'stash_plus_or_minus' var
'stash_plus_or_minus': stash_plus_or_minus,
},
dataType: 'json',
success: function (data) {
document.getElementById("stash_recipe_btn").innerHTML = data.stash_plus_or_minus;
$("#stash_recipe_tooltip").attr('data-tooltip', data.stash_tooltip);
}
});
}
function addClickHandlers() {
$("#stash_recipe_btn").click( add_recipe_to_stash );
}
</script>


It seems like

$("#stash_recipe_tooltip").attr('data-tooltip', data.stash_tooltip);


should be changing my tooltip, but nothing happens. Any idea how to successfully update the data-tooltip without refreshing the page? I've tried different variations of the above line but I can't get it to update.

Answer

You can use the tooltip function for that:

success: function (data) {
    document.getElementById("stash_recipe_btn").innerHTML = data.stash_plus_or_minus;
    $("#stash_recipe_tooltip").tooltip('tooltip', data.stash_tooltip);  
}
Comments