1

somehow addEventListener is not working for me. I am using following function. It shows me number of lines in the alert at line 8 but never says completed... Can anyone please tell the reason. I am using IE.

function load() {   
var lnk = new Array();
lnk = document.getElementsByTagName("a");
var len = lnk.length;
alert('inside for..length is ..'+len);

for (var i=0;i<len;i++){
alert(i+" "+lnk[i]);
    lnk[i].addEventListener('click',callMe,false);
        alert('completed');
}
}
Adam Lear
  • 38,111
  • 12
  • 81
  • 101
user837593
  • 337
  • 1
  • 5
  • 25

3 Answers3

2

Internet Explorer doesn't support addEventListener until version 9. Previous versions use the proprietary attachEvent.

It is generally a good idea to use a library that abstracts browser differences (or a more general library such as YUI or jQuery).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Internet Explorer does not implement the "addEventListener()" API :-)

You can use attachEvent() in IE.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Got it! attachEvent is working. Thanks. But now following code is not working. Can anyone help? function callMe() { var link = this; var id = link.getAttribute('id'); alert('Inside Onclick function...'+id); //Nothing is coming for id } someLink – user837593 Jul 10 '11 at 22:28
0

That is because each browser do it differently (thats the original reason js libraries where created). Read this

IE uses element.attachEvent('onclick',doSomething)

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278