We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

oronm on 09/05/06


Tagged

original addevent


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

vanne
vali29


addEvent


Published in: JavaScript 


URL: http://www.dustindiaz.com/top-ten-javascript/

Surely a staple to event attachment! Regardless to what version you use written by whatever developer, it does what it says it does. And of course as you might of known, I’ve put together quite a handy version myself recently of addEvent() with some help from the contest winner and Mark Wubben along with a few minor syntax adjustments. But just to be fair to Scott Andrew, here is the original that started it all.

  1. function addEvent(elm, evType, fn, useCapture) {
  2. if (elm.addEventListener) {
  3. elm.addEventListener(evType, fn, useCapture);
  4. return true;
  5. }
  6. else if (elm.attachEvent) {
  7. var r = elm.attachEvent('on' + evType, fn);
  8. return r;
  9. }
  10. else {
  11. elm['on' + evType] = fn;
  12. }
  13. }

Report this snippet 

You need to login to post a comment.