I want an alert to show up, after the second click. first in the li then in the div.button.
HTML
<li onclick="$(document).ShowPosts(4)"></li>
<div class="button">[show posts]</div>
this function has an inner ajax call to retrieve the number of posts. Once I successfully has returned the data with ajax, and click the button, the alert doesnt show up.
JQUERY
jQuery.fn.ShowPosts = function(id_user) {
$.ajax({
type: 'POST',
url: "posts.php",
data: {
'id_user': id_user
},
success: function(data) {
var total_posts = data;
});
$(".button").on('click', function(e) {
alert(total_posts);
}
});
}
It looks like the click event from buttom doesn't recognize the varia ble retrieved by ajax...what am doing wrong?
total_postsoutside the ajax. until the ajax will not succeed, thetotal_postwill beundefined.total_postsbefore the ajax call, for examplevar total_post=2000when clicking thediv.buttonit shows up an alert saying2000not the number retrieved by the ajax call.varbeforetotal_postsin thesuccesscallback.