Saulius Slavinskas Saulius Slavinskas - 1 month ago 9
Ajax Question

Jquery function doesn't work after Ajax call

I've got this function:

$(document).ready(function() {
$('.post_button, .btn_favorite').click(function() {


//Fade in the Popup
$('.login_modal_message').fadeIn(500);

// Add the mask to body
$('body').append('<div class="overlay"></div>');
$('.overlay').fadeIn(300);
return false;
});


My page loads content with favourite buttons, but after Ajax call and generated additional new content the function doesn't work when you click new content's buttons. What could be not right?

Answer

That is because you are using dynamic content.

You need to change your click call to a delegated method like on

$('.post_button, .btn_favorite').on('click', function() {

or

$("body").on( "click", ".post_button, .btn_favorite", function( event ) {