Search code examples
jquerylive

Any way to trigger the handler added later?


$('.kkk').click(function() {
  // Execute Event
});

$('body').append('<div class="kkk">Another target</div>');

Since it was added after the call to .click(), clicks on it will do nothing.

A> I wonder if Any way to trigger the handler(click) added later? except live()

Thank you~~~~


Solution

  • delegate() is exactly what you need for this. live() should be deprecated, in my opinion.

    $("body").delegate(".kkk", "click", function() {
    
        // Execute Event
    
    });
    

    EDIT : Seeing as how there's some discussion over live() vs delegate(), take a look at the posted answer to this question: Jquery live() vs delegate(). I think it's pretty clear.