1

I am building a web app, and the UI will have some dynamically created buttons. I struggled with click events on those buttons, but made it work by having click events on static tags that dynamically filled.

While this works, it doesn't seem like a great way to do this.

Is there a more semantic way of doing this? Or better still, a way to directly click the dynamically created button, and have a function triggered?

Mandu
  • 157
  • 1
  • 10
  • 2
    Learn [event delegation](http://learn.jquery.com/events/event-delegation/). Visit this [Might help](http://stackoverflow.com/questions/21389374/jquery-calling-a-function-from-a-button-created-with-a-function) – Satpal Jan 30 '14 at 19:26

1 Answers1

1

Use .on() method:

$(document).on('click', '.button-class', function() {
    // Clicked!
});
Oleg
  • 7,070
  • 4
  • 47
  • 49