In IE, you should use attachEvent , not the standard addEventListener .
A common practice is to check the addEventListener method is addEventListener and used, otherwise use attachEvent :
if (el.addEventListener){ el.addEventListener('click', modifyText, false); } else if (el.attachEvent){ el.attachEvent('onclick', modifyText); }
You can make a function:
function bindEvent(el, eventName, eventHandler) { if (el.addEventListener){ el.addEventListener(eventName, eventHandler, false); } else if (el.attachEvent){ el.attachEvent('on'+eventName, eventHandler); } } // ... bindEvent(document.getElementById('myElement'), 'click', function () { alert('element clicked'); });
You can run the example of the above code here .
The third argument to addEventListener is equal to useCapture ; if true, this means that the user wishes to initiate event capture .
CMS Nov 08 '09 at 4:40 2009-11-08 04:40
source share