Tip: Click lines to highlight, hold ctrl/cmd to multi-select

JavaScript event delegator template (10-Feb @ 13:48)

Template for event delegation

www.flickr.com-photos-bjarlestam

Syntax Highlighted Code

  1. window.onload = function () {    var navigation = document.getElementById("some_element_high_up_in_the_hierarchy");
  2.     navigation.onclick = function (evt) {
  3.         // Event tweaks, since IE wants to go its own way...
  4.         var event = evt || window.event;
  5.         var target = event.target || event.srcElement;
  6.         if(target.className && target.className==='someClass') {
  7.            //do your stuff here
  8.         }
  9.     }
  10. };

Plain Code

window.onload = function () {    var navigation = document.getElementById("some_element_high_up_in_the_hierarchy");
    navigation.onclick = function (evt) {
        // Event tweaks, since IE wants to go its own way...
        var event = evt || window.event;
        var target = event.target || event.srcElement;
        if(target.className && target.className==='someClass') {
           //do your stuff here
        }
    }
};

Permalink: http://codedumper.com/javascript-even-delegator-template