I am learning Jquery. I have a general question which is something I encounter quite a bit. Now, I know that similar questions have been posted before but none which I can directly apply to my situation. I have read the jQuery documentation without being able to glean a clear answer (but this is likely due to my novice status)
Here it is.
Lets say on your page you have a div, and elsewhere you have a button. You press the button, and new content loads into the div.
Now let say you have buttons in that div which THEMSELVES are linked to ajax calls. What must one do, to ensure that those buttons, when reloaded by the first ajax call, are bound to their own ajax calls properly.
Now let me be specific.
I have a div that hold photos of members that are following a question on my Q/A site. When I hover over those photos, via ajax, their info is pulled from a DB and displayed on a tooltip.
However, elsewhere on the same page is a 'follow this thread' button. If I press this, the div is reloaded, now with my photo in the div along with all the rest. But now the tooltip wont work until I refresh. I know its something to do with binding (or delegation) but I having great difficulty getting my head around it.
I fully get that this is an easy issue - but if someone could explain this I would be extremely grateful. Its an issue that crops up all around my site.
Here are my two functions,
My follow thread function:
$(document.body).on("click", "#follow", function(){
var followed_id = $('#followed_id').val();
$.ajax({
url: '../ajax/follow_this_member.php',
type: 'post',
data: {
'followed_id': followed_id
},
success: function(html){
$('.stats_member_profile').load('profile_stats.php', {id:followed_id});
$('.follow_and_message').load('follow_and_message_buttons.php', {id:followed_id});
}
});
return false;
});
My tooltip function (the qtip plugin)
$(function(){
$('img[data]').qtip({
content: {
text: function(event, api) {
$.ajax({
url:'../ajax/tooltip_members.php', // Use data-url attribute for the URL
method: 'post',
data: {
id: $(this).attr('data')
}
})
.then(function(content) {
// Set the tooltip content upon successful retrieval
api.set('content.text', content);
}, function(xhr, status, error) {
// Upon failure... set the tooltip content to the status and error value
api.set('content.text', status + ': ' + error);
});
return 'Loading...'; // Set some initial text
}
},
style: { classes: 'member_summary_box_style' }
});
});