Return to Snippet

Revision: 3275
at July 3, 2007 10:12 by rhlowe

Updated Code
// Cross-browser implementation of element.addEventListener()

function listen(evnt, elem, func) {
    if (elem.addEventListener)  // W3C DOM
    else if (elem.attachEvent) { // IE DOM
         var r = elem.attachEvent("on"+evnt, func);
	return r;
    else window.alert('I\'m sorry Dave, I\'m afraid I can\'t do that.');

// Use: listen("event name", elem, func);

Revision: 3274
at July 2, 2007 11:48 by rhlowe

Initial Code
function listen(event, elem, func) {
    elem = $(elem);
    if (elem.addEventListener)  // W3C DOM
    else if (elem.attachEvent) { // IE DOM
         var r = elem.attachEvent("on"+event, func);
	return r;
    else throw 'No es posible añadir evento';

// Use:
// listen("event", elem, func);

Initial URL

Initial Description
Snatched this from another post and fixed something I found was not working.

Initial Title
Cross Browser Add Event Listener

Initial Tags

Initial Language