0

I have a problem with my code, I have some buttons which execute a function when they are called.

<button class="executeFucntion" > Example </button>

That one works, but I have other button which create buttons dynamically:

 $("<button class='executeFucntion'>Example</button>").insertBefore("#id")

This buttons, the second ones, don't work with the next function and I don't know why:

$(".executeFucntion").on("click", function(e) {
//Code here
});

Any help? Thanks.

TiGreX
  • 1,602
  • 3
  • 26
  • 41
  • Pick your favorite: [Jquery even handler not working on dynamic content](http://stackoverflow.com/questions/15090942/jquery-even-handler-not-working-on-dynamic-content) or [Event binding on dynamically created elements?](http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – apsillers Jun 12 '15 at 15:58

1 Answers1

4

For dynamically added elements use event delegation.

$(document).on("click", ".executeFucntion", function (e) {
    //Code here
});

Event delegation allows us to attach a single event listener, to a parent element, that will fire for all descendants matching a selector, whether those descendants exist now or are added in the future.

Tushar
  • 85,780
  • 21
  • 159
  • 179