Results 1 to 3 of 3

Thread: expanding menu

  1. #1
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question expanding menu

    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/java...expanding-menu)
    Code:
    <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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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:

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking

    thanks for pointing that out, it now works perfectly!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •