0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yalmaz Khalil
  • 21
  • 1
  • 2
  • 6
  • 4
    You shouldn't test for an unrelated property (`window.event`). Instead, check for `addEventListener` itself. – SLaks Nov 20 '11 at 00:21
  • trye using document. i don't think the window hase a onload. Plus try jquery.. – Bram Nov 20 '11 at 00:22

5 Answers5

4

You misspelled function.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Try this:

function attachEvent(element, event, callbackFunction) {
    if (element.addEventListener) {
        element.addEventListener(event, callbackFunction, false);
    } else if (element.attachEvent) {
        element.attachEvent('on' + event, callbackFunction);
    }
};
Hristo
  • 45,559
  • 65
  • 163
  • 230
0

Instead of working this way, try this:

Element.prototype.events = {};
Element.prototype.addEvent = function (event, callBack) {
    if (this.events["on" + (event.replace("on", ""))] == undefined) {
        this.events["on" + (event.replace("on", ""))] = [];
    }
    this.events["on" + (event.replace("on", ""))].push(callBack);
    if (this["on" + (event.replace("on", ""))] == undefined) {
        this["on" + (event.replace("on", ""))] = function (e) {
            for (i in this.events["on" + (event.replace("on", ""))]) {
                this.events["on" + (event.replace("on", ""))][i](e);
            }
        }
    }
}

You will use it this way:

document.body.addEvent("click",function(){
   alert("Test");
});
document.body.addEvent("click",function(){
   alert("Test 2");
});
document.getElemntById("inputtest").addEvent("keyup",function(){
   alert("Input keyup");
});
Guilherme Ferreira
  • 2,209
  • 21
  • 23
0
typeof (window.event) != 'undefined'

doesn't work really on chrome when I tested your code! this would be work fine:

!!window.attachEvent

and i consider that using brace in if..esle is a good practice.

function addEvent(elem, event, func ) {
    if (!!window.attachEvent){

        elem.attachEvent('on' + event, func);
    }
    else{
       elem.addEventListener(event, func, false);
    }
}
island205
  • 1,730
  • 17
  • 28
-3

Please try this.

function addEvent(elem,event,func)
{
    var evn = "on"+event;
    elem[evn] = func;
}
var obj = document.getElementById("link");
addEvent(obj,"click",function(){alert('hi');});

Here using this code you need not worry about attachEvent or addEventListener. This code will work for all browser. Hope this solves your problem.

AmGates
  • 2,127
  • 16
  • 29