PDA

View Full Version : expanding menu



Agrajag
02-24-2006, 11:13 AM
hi
i'm trying to create menu boxes which will retract and expand when the title for that box is clicked on. i have some code which seems to me that it should work, but it doesn't.

here is my code (from http://www.tutorio.com/tutorial/javascript-expanding-menu)


<script type="text/javascript">
function switchit(list){
var listElementStyle=document.getElementById(list).style;
if (listElementStyle.display=="none"){
listElementStyle.display="block";
else {
listElementStyle.display="none";
}
}
</script>


an example can be found at http://www.freewebs.com/agrajag/

thanks

jscheuer1
02-24-2006, 05:47 PM
Your code has no closing } for the function and the way curly brackets are used in it does not agree with the obvious intent of the function:


<script type="text/javascript">
function switchit(list){ // this bracket has no closing bracket
var listElementStyle=document.getElementById(list).style;
if (listElementStyle.display=="none"){
listElementStyle.display="block";
else {
listElementStyle.display="none";
}
}
</script>

In the copy of your code above with added comment and colors, brackets of like color open and close for each other while the commented one is an orphan. The whole thing could be written like so:


<script type="text/javascript">
function switchit(list){
var listElementStyle=document.getElementById(list).style;
if (listElementStyle.display=="none")
listElementStyle.display="block";
else
listElementStyle.display="none";
}
</script>

There could be other problems. Most notably, if the element that you are toggling the display of does not support the 'block' value in all browsers, this will make it behave oddly in those browsers.

Agrajag
02-24-2006, 09:54 PM
thanks for pointing that out, it now works perfectly!