Jquery On Click method not working for newly added Div

I wrote a code that on a button click adds a new tweet (div) to the div list. See the abbreviated code below. Now, even if the new div has all the same classes as the other divs. It doesn't execute when clicked.

How can I get jquery click method to work for newly added divs?

$('.buttonClass').on('click', function() {

    ....
              $newTweet.insertAfter('button');
    });



$('.douglascalhoun').on('click', function() {
          $("div").slideToggle();
          $("div.douglascalhoun").slideDown();
        });

      

+3


source to share


3 answers


You need to use event delegation to bind events to dynamically generated elements:



$('body').on('click', '.douglascalhoun',function() {
      $("div").slideToggle();
      $("div.douglascalhoun").slideDown();
    });

      

+3


source


behind curtain event delegation works for dynamically generated elements



$(staticAncestors).on(eventName, dynamicChild, function() {
  // Your Function
});

      

0


source


Use the below code when you execute add events dynamically.

$(document).on('click', 'selector', function(){
     //from dynamic div
})

      

0


source







All Articles