Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Created January 18, 2012 21:39
Show Gist options
  • Save JeffreyWay/1635889 to your computer and use it in GitHub Desktop.
Save JeffreyWay/1635889 to your computer and use it in GitHub Desktop.
Legacy JS
// THE GOAL
// Write $('ul').on('click', 'a', fn); in JavaScript
// Must support old IE (so no Selectors API (matchesSelector) or anything)
// Can you shorten this?
var addEvent = (function () {
if (window.addEventListener) {
return function (el, ev, fn) {
el.addEventListener(ev, fn, false);
};
} else {
return function (el, ev, fn) {
el.attachEvent('on' + ev, function() {
return fn.call(el, window.event);
});
};
}
}());
var uls = document.getElementsByTagName('ul');
for ( var i = 0, len = uls.length; i < len; i++ ) {
addEvent(uls[i], 'click', function(e) {
var target = e.target || e.srcElement;
if ( target && target.nodeName.toLowerCase() === 'a' ) {
// proceed
}
});
}
@jwmcpeak
Copy link

@eliperelman Not by default, but the addEvent() implementation above takes care of that.

fn.call(el, window.event);

@eliperelman
Copy link

You are correct. My fork operates differently, so I didn't take that into consideration. Also the variable I commented before is the wrong one. :)

@eliperelman
Copy link

@JeffreyWay
Copy link
Author

@jeremy - Oh yeah, forgot about that.

@mattneary
Copy link

Tweet sized addEvent function:

a=(function(a,b)function(c,d,e)a[b]?c[b].call(c,d,e):c.attachEvent("on"+d,function()e.call(c,a.event)))(window,"addEventListener")

Note that JavaScript 1.8 is required for the function shorthand. Here it is unminified

var addEvent = (function (w,ael) {
    return function (el, ev, fn) {
        w[ael] ? el[ael].call(el, ev, fn) :     
        el.attachEvent('on' + ev, function() {
            return fn.call(el, w.event);
        });
    };
}(window,"addEventListener"));

@heavensloop
Copy link

This might also work fine:

 1 var addEvent = addEV;
 2 function addEV(){
 3     return function (el, ev, fn) {
 4         if (window.addEventListener)
 5             el.addEventListener(ev, fn, false);
 6         else{
 7             el.attachEvent('on' + ev, function() {
 8                 return fn.call(el, window.event);
 9             });
10         }
11      };
12 }
13 var uls = document.getElementsByTagName('ul');
14 
15 for ( var i = 0, len = uls.length; i < len; i++ ) {
16     addEvent(uls[i], 'click', function(e) {
17         if ( e.target && e.target.nodeName.toLowerCase() === 'a' ) {
18             // proceed
19         }
20     });
21 }
22 

@JeffreyWay
Copy link
Author

Or, if we need multiple element event binding...

var addEvent = (function () {
var filter = function(el, type, fn) {
    for ( var i = 0, len = el.length; i < len; i++ ) {
        addEvent(el[i], type, fn);
    }
};
if ( document.addEventListener ) {
    return function (el, type, fn) {
        if ( el && el.nodeName || el === window ) {
            el.addEventListener(type, fn, false);
        } else if (el && el.length) {
            filter(el, type, fn);
        }
    };
}

return function (el, type, fn) {
    if ( el && el.nodeName || el === window ) {
        el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
    } else if ( el && el.length ) {
        filter(el, type, fn);
    }
};

})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment