views:

66

answers:

2
+1  Q: 

Drop Down Problems

I am trying to create a drop down list. I have it working but not fully, using this code:

$(document).ready(function(){
   $("#zone-bar li em").hover(function() {
       var hidden = $(this).parents("li").children("ul").is(":hidden");

       $("#zone-bar>ul>li>ul").hide()        
       $("#zone-bar>ul>li>a").removeClass();

       if (hidden) {
           $(this).parents("li").children("ul").toggle()
                  .parents("li").children("a").addClass("zoneCur");
       } 
   });
});

I managed to make it work so on hover the drop down list will appear, but when you move to select one of the items from the drop down list the drop down list closes. How do I fix that?

It works if I put it to onclick but then you have to click the arrow to close it again. You can see a live example at http://doctorwhohd.com (currently using onclick)

A: 

Try putting the hover on

  #zone-bar li  

and not on the em

Update, yes, you would need to modify the script:

$("#zone-bar li").hover(function() {
   var hidden = $(this).children("ul").is(":hidden");

   $("#zone-bar>ul>li>ul").hide()        
   $("#zone-bar>ul>li>a").removeClass();

   if (hidden) {
       $(this).children("ul").toggle()
              .children("a").addClass("zoneCur");
   } 

});

However, the suggestion of using mouseenter is probably superior, it seems that this causes momentary flicker.

You might want to consider doing this with pure CSS, there is no obvious reason to employ jquery to create this effect.

#zone-bar li{ height:1em; overflow:hidden}; 
#zone-bar li:hover{ height:auto}; 
unomi
when i use this nothing happens when i hover over it it is currently been left on .hover so you can see what it is like with the em
+1  A: 

It is likely behaving this way because hover() is intended to take 2 functions. One for mouseenter and one for mouseleave.

When you give it only one, I think it fires the one for both events.

Instead of hover(), use mouseenter().

$("#zone-bar li em").mouseenter(function() {
   var hidden = $(this).parents("li").children("ul").is(":hidden");

   $("#zone-bar>ul>li>ul").hide()        
   $("#zone-bar>ul>li>a").removeClass();

   if (hidden) {
       $(this).parents("li").children("ul").toggle()
              .parents("li").children("a").addClass("zoneCur");
   } 
});
patrick dw
thanks you for that it works great one more thing would it be possible to then make it so when the mouse moves out of the drop down menu it disappears because at the moment when the mouse leaves the drop down box the menu's still stays
Try placing a `.mouseleave` handler on `$("#zone-bar>ul>li>ul")` to make it hide when you `mouseleave`. Or, wrap each menu combination in a container, and place a `hover()` event on that container (with both required handler functions). That way the `mouseleave` part of `hover()` will only fire when leaving the container.
patrick dw
thank you so much the first idea worked fine i used the .mouseleave and it works great! thanks for your time.
@user - You're welcome. :o)
patrick dw