[thelist] onclick event

Matt Warden mwarden at gmail.com
Wed Dec 20 16:28:29 CST 2006


On 12/20/06, Fred D Yocum <fdy at mcc.org> wrote:
> Apologies for the length of this post. I have been going around in circles
> for about a day now.
>
> I have a nested set of ordered lists which has six levels -- it is a sort
> of index of about six hundred document/document fragments.
>
> I want my DOM script to  check each ordered list to see whether it is
> contained in a list item (li). If it is nested in a list item, then set
> the display for the ordered list to hidden and add an element, to the
> parent list item. This element when clicked will reset the display of the
> nested list to "block", making it visible. This way people can click their
> way deeper into the index, making each level visible when they need it.
>
> Every thing works except the onclick which appears to do nothing... I
> tried a direct statement
>  downObject.onclick = thisOL.style.display="block";//hide the existing
> list

Your primary issue is the scope of your thisOL variable. When the
event fires, JavaScript (essentially) sees only this:

function () {
    thisOL.style.display="block";
}

thisOL is defined nowhere in that function. You should get an error
(or, if you're not getting an error, then thisOL refers to the last
element in the array, because that's what it was last set to).

There are a number of ways to get around this. First, the way I would
suggest is to use YUI or a similar utility library. This will also
help you with the class name management (don't use
setAttribute('class', ...)). You can find YUI here: http://yui.sf.net
and the info on the Event utility here:
http://developer.yahoo.com/yui/event/#scope

Alternatively, you can get the event's source element from the event
object. Here's an example:
http://mwarden.f2o.org/sandbox/getidfromeventhandler.js

Alternatively, you can look into using call() or apply():
http://www.webreference.com/js/column26/apply.html

Hope this helps,

-- 
Matt Warden
Cleveland, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.



More information about the thelist mailing list