Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 494
Saulius s Avatar asked Jan 07 '14 01:01

Saulius s


1 Answers

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 ) {
like image 105
Scary Wombat Avatar answered Sep 21 '22 03:09

Scary Wombat