I have a simple existing CSS rollover menu that I've recently added sub-level options to, using Javascript to expand a group with one click using the 'show' command and another click to contract it again with the 'hide' command.

I'm a total newbie to Javascript, so was wondering if anyone out there knew of an easy way to automatically contract all of the expanded groups when clicking on one of the other unexpanded ones - as opposed to having to 'hide' everything manually?

Any help or advice that anyone can offer greatfully appreciated!

03-13-2009, 03:00 PM

Eldarrion

Assign a class upon doing a show... not sure which framework we're discussing, but considering the topic, I'm guessing jQuery? Or is it something else? Anyways... after assigning a class on show, you can easily call that class and hide everything that has it, then clear it and after that assign it to the newly opened menus.

03-13-2009, 04:52 PM

srlagarto

Quote:

Originally Posted by Eldarrion

Assign a class upon doing a show... not sure which framework we're discussing, but considering the topic, I'm guessing jQuery? Or is it something else? Anyways... after assigning a class on show, you can easily call that class and hide everything that has it, then clear it and after that assign it to the newly opened menus.

Thanks for the quick response, but as a newbie I'm not entirely sure how to apply your recommendation! My Javascript code is from a Tutorial at tutorials-db.com, but doesn't seem to say what framework it is :

Nope, no framework involved, looks like clean JS. Either way, as it seems, you are already assigning the class there with .className Now for the more interesting part... finding each element that is currently visible and hiding it:

Probably _not_ the best way to do it... and I'm not sure exactly how it will play out with nested locations, but I believe it can be modified to ignore elements that are parent elements of the one you're currently trying to show.

03-13-2009, 05:33 PM

Eldarrion

And here is the code that will take into account for a currently visible parentNode and avoid hiding it:

Should work like a charm... with 1-level nesting. Once again, might be a better way. (I certainly know a better one using jQuery, but not pure JS) Though for the purposes, this one should work.

Wow - that first script DOES work like a charm - thanks! I can't seem to get this second one to work, though - am I doing something wrong? Just seems to be the last few lines that are different, but for whatever reason the script appears to stop working altogether.

I'm very grateful for your prompt assistance and would certainly be interested in learning more about jQuery, if you feel that is the better framework to use - any pointers for resource / tutorials?