1

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.

YsoL8
  • 2,186
  • 5
  • 29
  • 47
  • 1
    As I already commented at your previous question, you should read http://www.quirksmode.org/js/events_advanced.html – Felix Kling Jul 04 '11 at 12:55

2 Answers2

2

Have you tried

elementsList[i].previousSibling.lastChild.attachEvent
("onclick", (function(el){return function(){toggle(el)};})(elementsList[i]),false);
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • How did that work?????? I've been trying similiar variations for hours. I'm certain I tried that already! – YsoL8 Jul 04 '11 at 12:50
  • attachEvent takes two arguments: event name and function, so "false" is unnecessary – anton_byrna Jul 04 '11 at 12:51
  • Does `el` actually refer to the element that was clicked in IE? - trying to find specs on what parameters are expected in the handler. – scunliffe Jul 04 '11 at 13:00
  • e.g. I thought you needed... `function(event){var el = event.srcElement;....` attaching:http://msdn.microsoft.com/en-us/library/ms536913%28v=VS.85%29.aspx event object:http://msdn.microsoft.com/en-us/library/ms535863%28v=VS.85%29.aspx – scunliffe Jul 04 '11 at 13:09
0

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

scunliffe
  • 62,582
  • 25
  • 126
  • 161