8

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?

Saulius s
  • 581
  • 2
  • 8
  • 18

7 Answers7

16

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 ) {
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
7

Instead of this:

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

do this:

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

on will work with present elements and future ones that match the selector.

Cheers

Edgar Villegas Alvarado
  • 17,527
  • 1
  • 39
  • 59
  • Good job explaining why using `on` solves the issue. Using `$(document)` was also a change that solved this same issue for me. – GWR Nov 04 '15 at 15:45
1

class-of-element is the applied class of element. which is selector here.

  $(document).on("click", ".class-of-element", function (){
    alert("Success");
  });
Satyam Pathak
  • 4,618
  • 1
  • 17
  • 43
Pergin Sheni
  • 175
  • 1
  • 11
0

I am not sure if I am getting your question right but you may want to try..

$.ajax({
  url: "test.html"
}).done(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;
});

Just try to paste your code inside done function.

Hope it helps :)

EDIT: I also notice you are missing }); on your question.

HTTP
  • 1,478
  • 3
  • 14
  • 21
0

If you know the container for .post_button, .btn_favorite then use

$('#container_id').on('click', '.post_button, .btn_favorite', function () { });

so if '.post_button, .btn_favorite' are not found then it will bubble up to container_id

else if you don't know the container then delegate it to document

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

Reference

Raunak Kathuria
  • 3,025
  • 1
  • 14
  • 25
0

The following worked for me

    $(document).ready(function(){ 
         $(document).bind('contextmenu', function(e) {
            if( e.button == 2 && jQuery(e.target).is('img')) {
              alert('These photos are copyrighted by the owner. \nAll rights reserved. \nUnauthorized use prohibited.'); 
              return false;
            }
         });
    });
0

You need to bind the jQuery click event once your ajax content is replaced old content

in AJAX success block you need to add code like here new response html content one a tag like

<a href="#" id="new-tag">Click Me</a>

So you can bind the new click event after change the content with following code

$("#new-tag").click(function(){
   alert("hi");
   return false;
});
Mitul
  • 3,543
  • 2
  • 18
  • 34