Results 1 to 3 of 3

Thread: Hide list item when another is selected

  1. #1
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hide list item when another is selected

    This bit of javascript almost does what I want. It toggles the display of submenus when a list item is clicked. What I would also like is to hide the submenu that is showing when another selection is made. I just can't figure out how, and would appreciate some help.
    Code:
    function toggle() {
    var el=document.getElementById("menu").getElementsByTagName("li");
      for (i=0; i<el.length; i++) {
        el[i].className = "hide";
        el[i].onclick = function() {
        this.className = (this.className == "hide") ? "show" : "hide";
        }
      }
    }
    Then in the CSS I have defined the properties for the classes to hide the submenus until selected, and the markup is just a series of nested uls with the top level ul given the id "menu".

    Also I was wondering if its possible to make it work on mouseover/mouseout as well as onclick.
    Last edited by billyboy; 11-27-2006 at 01:27 PM.

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by billyboy View Post
    Then in the CSS I have defined the properties for the classes to hide the submenus until selected, and the markup is just a series of nested uls with the top level ul given the id "menu".
    How deeply nested are these lists?

    The most elegant solution is to retain a reference to the "expanded" list. This is trivial for only one "level", but slightly - and only slightly - more involved for multiple levels.

    Also I was wondering if its possible to make it work on mouseover/mouseout as well as onclick.
    Sure, but one thing at a time.

    Mike

  3. #3
    Join Date
    Dec 2005
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Mike, thanks for the response.

    If possible I'd like something that's adaptable to different situations, that would work regardless of the number of levels of sub menus.

    I had been using the following code, which does that. But it doesn't take into account keyboard navigation, so I tried adding functions for onfocus/onblur by copying the existing onmouseover/onmouseout functions. Onblur didn't hide the submenus again though (Am I misunderstanding what onblur does, or was I doing something wrong?) So I tried a different approach and was hoping I could put it all together into one. I got as far as the first code I posted and got stuck:
    Code:
    function hideSubMenus() {
    var el=document.getElementById("menu").getElementsByTagName("li");
      for (i=0; i<el.length; i++) {
        el[i].className = "hide";
        el[i].onmouseover = function() {
          this.className = "show";
        }
        el[i].onmouseout = function() {
          this.className = "hide";
        }
      }
    }
    Last edited by billyboy; 11-28-2006 at 06:09 PM.

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
  •