Revision: 31018
Updated Code
at August 31, 2010 02:31 by Nettuts
Updated Code
var addEvent = (function( window, document ) { if ( document.addEventListener ) { return function( elem, type, cb ) { if ( (elem && !elem.length) || elem === window ) { elem.addEventListener(type, cb, false ); } else if ( elem && elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } else if ( document.attachEvent ) { return function ( elem, type, cb ) { if ( (elem && !elem.length) || elem === window ) { elem.attachEvent( 'on' + type, function() { return cb.call(elem, window.event) } ); } else if ( elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } })( this, document ); // Example Usage var lis = document.getElementsByTagName('li'); addEvent( window, 'click', function() { this.style.border = '1px solid red'; });
Revision: 31017
Updated Code
at August 31, 2010 02:26 by Nettuts
Updated Code
var addEvent = (function( window, document ) { if ( document.addEventListener ) { return function( elem, type, cb ) { if ( (elem && !elem.length) || elem === window ) { elem.addEventListener(type, cb, false ); } else if ( elem && elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } else if ( document.attachEvent ) { return function ( elem, type, cb ) { if ( elem && !elem.length ) { elem.attachEvent( 'on' + type, function() { return cb.call(elem, window.event) } ); } else if ( elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } })( this, document ); // Example Usage var lis = document.getElementsByTagName('li'); addEvent( window, 'click', function() { this.style.border = '1px solid red'; });
Revision: 31016
Updated Code
at August 28, 2010 05:27 by Nettuts
Updated Code
var addEvent = (function( window, document ) { if ( document.addEventListener ) { return function( elem, type, cb ) { if ( elem && !elem.length ) { elem.addEventListener(type, cb, false ); } else if ( elem && elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } else if ( document.attachEvent ) { return function ( elem, type, cb ) { if ( elem && !elem.length ) { elem.attachEvent( 'on' + type, function() { return cb.call(elem) } ); } else if ( elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } })( this, document); // Example Usage var lis = document.getElementsByTagName('li'); addEvent( lis, 'click', function() { this.style.border = '1px solid red'; });
Revision: 31015
Updated Code
at August 27, 2010 09:23 by Nettuts
Updated Code
var addEvent = (function( ) { if ( document.addEventListener ) { return function( elem, type, cb ) { if ( elem && !elem.length ) { elem.addEventListener(type, cb, false ); } else if ( elem && elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } else if ( document.attachEvent ) { return function ( elem, type, cb ) { if ( elem && !elem.length ) { elem.attachEvent( 'on' + type, function() { return cb.call(elem) } ); } else if ( elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } }; } })(document, window); // Example Usage var lis = document.getElementsByTagName('li'); addEvent( lis, 'click', function() { this.style.border = '1px solid red'; });
Revision: 31014
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 27, 2010 09:14 by Nettuts
Initial Code
var addEvent = function( elem, type, cb ) { if ( document.addEventListener ) { if ( elem && !elem.length ) { elem.addEventListener(type, cb, false ); } else if ( elem && elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } } else if ( document.attachEvent ) { if ( elem && !elem.length ) { elem.attachEvent( 'on' + type, function() { return cb.call(elem) } ); } else if ( elem.length ) { var len = elem.length; for ( var i = 0; i < len; i++ ) { addEvent( elem[i], type, cb ); } } } }; // Example Usage var lis = document.getElementsByTagName('li'); addEvent( lis, 'click', function() { this.style.border = '1px solid red'; });
Initial URL
Initial Description
Initial Title
JS Cross-Browser Event Binding
Initial Tags
Initial Language
JavaScript