JavaScript Forum

Problem with addEventListener in firefox

Dec 4, 2009 1:42 pm
Generale Cluster

Hello,
I have an unordered list with sublists in it and I've used it to make an
expandable tree menu.
This is my sample page: http://tinyurl.com/y88ouj8
When I click on "prodotti", I get that "Clienti" submenu expands. I
would expect the "prodotti" menu to expand when clicking it.
My problem is that when I call addEventListener in firefox, it seems to
attach the listener always to the same element.
What is the problem in the code?
Thank you
Regards

elimina unsampdoriano per rispondere via email
Dec 7, 2009 10:57 am
Matthias Reuter
Re: problem with addEventListener in firefox

Generale Cluster wrote:

> My problem is that when I call addEventListener in firefox, it seems to
> attach the listener always to the same element.
> What is the problem in the code?

That is the code:

function hideByClassName(clName) {
list = document.getElementsByTagName("ul");
for (i=0;i<list.length;i++) {

if (list[i].className==clName) {
var oText = document.createTextNode("+");
list[i].style.display="none";
var mynode=list[i];

list[i].parentNode.insertBefore(oText,list[i].parentNode.childNodes[0]);
try {
list[i].parentNode.childNodes[1].addEventListener("click",
function(evt){toggle(mynode,evt)}, true);
} catch (err) {
list[i].parentNode.attachEvent("onclick",
function(evt){toggle(mynode,evt)});
}
}
}
}

The problem lies here:

function(evt){toggle(mynode,evt)}

mynode is a reference. You expect it to point to the element it referenced
when you added the event listener, but is is evaluated when the handler is
called. Therefore mynode points to the last item of list.

A solution is to extract the target in toggle:

function toggle (e) {
var event = e || window.event;
var target = e.target || e.srcElement;

// ...
}


Matt


Dec 7, 2009 9:10 pm
Generale Cluster
Re: problem with addEventListener in firefox

Matthias Reuter ha scritto:
> Generale Cluster wrote:
>
>> My problem is that when I call addEventListener in firefox, it seems
>> to attach the listener always to the same element.
[CUT]
> The problem lies here:
>
> function(evt){toggle(mynode,evt)}
>
> mynode is a reference. You expect it to point to the element it
> referenced when you added the event listener, but is is evaluated when
> the handler is called. Therefore mynode points to the last item of list.
[CUT]
> Matt

thank you
regards

elimina unsampdoriano per rispondere via email


Previous Thread: Baffling Results from my Javascript Class(-ishness) (Ninjas explain!)
Next Thread: File input problem

Related Forum Topics
.addEventListener('click', ...) on firefox 3
Hi,

I can't make this code work on *this* web client:
Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.0.13) Gecko/2009082121
Iceweasel/3.0.6 (Debian-3.0.6-1)


I want to add a 'click' event to an elem of this kind:
<div id="xx">taratata</div>, but this element is not
in the HTML file : it...
Dynamic iframe caching problem workaround needed for Firefox
There is a long-standing bug in Firefox whereby iframes, created by
scripting, display incorrectly cached content on reload
https://bugzilla.mozilla.org/show_bug.cgi?id=279048.

In my case, I have a page containing several iframes, and on load they
sometimes get muddled up (content loaded...