In jQuery, to hang a handler on all the links on the page is enough of such code
$( "a" ).click(function() { alert( "Handler for .click() called." ); }); Moreover, if you dynamically add a new <a></a> element to the page, then the event will also work on hiring.
I want to understand how this is done. On pure JS it turns out like this:
var a = d.getElementsByTagName("a"); for(var i = 0; i < a.length; i ++) { a[i].addEventListener("click", function (e) { alert( "Handler for .click() called." ); }); } But thus the event is hung up only on elements that already exist and if you add one more dynamically, there will be no event handler on it.
How to fix it? And how is this moment implemented in jQuery?