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

2 Replies to “Trigger event on dynamically created elements”

  1. Pingback: cenforce black 200

Comments are closed.