Trigger event on dynamically created elements

Event binding

When you add a new HTML element to the page dynamically, you will have to delegate an event handler that has the advantage of processing events from elements that are added to the document afterwards.

Jquery solution:
$(document).on( 'click', '.element', function () { ... });

Javascript solution:
document.addEventListener('click', function (e) { ... }, false);

Result Box
  • html
  • css
  • js

Share This:

Feel free to leave a comment!

You might alos be interested in: