YsoL8
YsoL8

Reputation: 2214

Javascript converting addEventListener to attachEvent

I have the following code calling a Javascript function in a nice shiny standards complient manner :). Please note that I must send elementsList[i] to the function (the this keyword will not be adaquate) as the event listerner is being attached to it's nephew (for want of a better term)

This rather mangled code will effectively find the control element for the dynamic behaviour of the current node in elementsList[i] and add a click event listener to it. When fired it passes the clicked node to the toggle function.

elementsList[i].previousSibling.lastChild.addEventListener
("click", (function(el){return function(){toggle(el)};})(elementsList[i]),false); 

Thing is it doesn't work at all with IE8 and below, and in spite of spending most of the morning trying to find a work around I just can't get it to play ball. If someone knows how to translate this into IE crapo code, I'd be grateful to see it.

Upvotes: 1

Views: 274

Answers (2)

scunliffe
scunliffe

Reputation: 63676

2 potential suggestions.

1.) Include jQuery in your project and use their x-browser event bindings (or mootools or some other lib)

2.) Not so-recommended, roll your own x-browser event bindings

Upvotes: 0

sushil bharwani
sushil bharwani

Reputation: 30197

Have you tried

elementsList[i].previousSibling.lastChild.attachEvent
("onclick", (function(el){return function(){toggle(el)};})(elementsList[i]),false);

Upvotes: 2

Related Questions