3

I have written some code that on the click of a button adds a new tweet(div) to a list of divs. See shortened code below. Now even though the new div has all the same classes as the other divs. It does not perform when clicked.

How can I get the 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();
        });
realsimonburns
  • 105
  • 1
  • 10

3 Answers3

3

You need to use event delegation for attaching events to dynamically generated elements:

$('body').on('click', '.douglascalhoun',function() {
      $("div").slideToggle();
      $("div.douglascalhoun").slideDown();
    });
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

behind the curtains event delegation works as follows for dynamically generated elements

$(staticAncestors).on(eventName, dynamicChild, function() {
  // Your Function
});
Nilesh Mahajan
  • 3,506
  • 21
  • 34
0

Use the below code, when you are performing events when added dynamically.

$(document).on('click', 'selector', function(){
     //from dynamic div
})
stanze
  • 2,456
  • 10
  • 13