JQuery live () in plain JavaScript?

I am trying to accomplish what the jQuery live () function can do, but in plain JavaScript. Can anyone here help with this?

Thanks!

+6
jquery live
source share
2 answers

Here is a small launch example

document.onclick = function(evt){ evt = evt || window.event; var element = evt.target || evt.srcElement; }; 

wherever you are, you get a link to the item that got the click.

More useful, however, in a real scenario would be to use the attachEvent method for IE, or addEventListener for the rest.

+2
source share

Something like that:

 myLive("div", "click", function() { ... }); 

 var liveArray = []; function myLive(selector, type, handler) { liveArray.push([selector, type, handler]); } // this handler should fire for any event on the page, and should be attached // to the document node function documentAnyEvent(e) { var e = e || window.event; var target = e.target || e.srcElement; for (var i = 0; i < liveArray.length; i++) { if (target mathes the selector AND e.type matches the type) { // fire the handler liveArray[i][2] } } } 
+2
source share

All Articles