JavaScript addEvent function
Posted
by
Yalmaz Khalil
on Stack Overflow
See other posts from Stack Overflow
or by Yalmaz Khalil
Published on 2011-11-20T00:19:04Z
Indexed on
2011/11/20
1:51 UTC
Read the original article
Hit count: 272
JavaScript
|javascript-events
I have an addEvent
function:
function addEvent(elem, event, func ) {
if (typeof (window.event) != 'undefined')
elem.attachEvent('on' + event, func);
else
elem.addEventListener(event, func, false);
}
<a href="#" id="link">link</a>
and I'm trying to add the following to window.onload
:
addEvent(window, 'load', function (){
// add another event
var link= document.getElementById('link');
addEvent(link, 'click', function () {alert('Hi'); });
});
My question is: why does the link
event not work?
© Stack Overflow or respective owner