0

how can i change this code so that the menu drops down when the ("ul.topnav li") is hovered and not the <span>. when i changed the code all the ("ul.subnav) dropped when hovered over.

thanks,

<script language="javascript" type="text/javascript">
 $(document).ready(function() {
 $("ul.subnav").parent().append("<span></span>");
 //When trigger is clicked...
 $("ul.topnav li span").mouseenter(function() { 
 //Following events are applied to the subnav itself 
 //(moving subnav up and down)
 //Drop down the subnav on click
 $(this).parent().find("ul.subnav").slideDown('fast').show(); 
 $(this).parent().hover(function() {
 }, function() {
 //When the mouse hovers out of the subnav, move it back up
 $(this).parent().find("ul.subnav").slideUp('slow'); 
 });
 //Following events are applied to the trigger 
 }).hover(function() { //(Hover events for the trigger)
 $(this).addClass("subhover"); //On hover over, add class "subhover"
 }, function() { //On Hover Out
 //On hover out, remove class "subhover"
 $(this).removeClass("subhover"); 
 });
 });
</script>
Jim Rubenstein
6,9605 gold badges38 silver badges55 bronze badges
asked May 6, 2011 at 18:57
1
  • I think that you are selecting the parent of the ul nav which is sliding all of the sub navs to slide down this.slidetoggle() Commented May 6, 2011 at 19:02

1 Answer 1

2

When you change the .mouseenter() event attachment to the li, it changes the scope of everything inside the function. Fortunately, all the code within just tries to get back up to the parent li of the span it was originally targeting. So, to fix this. you just remove the calls to parent() where appropriate.

<script language="javascript" type="text/javascript">
 $(document).ready(function() {
 $("ul.subnav").parent().append("<span></span>");
 //When trigger is clicked...
 $("ul.topnav li").mouseenter(function() { 
 //Following events are applied to the subnav itself 
 //(moving subnav up and down)
 //Drop down the subnav on click
 $(this).find("ul.subnav").slideDown('fast').show(); 
 $(this).parent().hover(function() {},
 function() {
 //When the mouse hovers out of the subnav, move it back up
 $(this).find("ul.subnav").slideUp('slow'); 
 });
 //Following events are applied to the trigger 
 }).hover(function() { //(Hover events for the trigger)
 $(this).addClass("subhover"); //On hover over, add class "subhover"
 }, function() { //On Hover Out
 //On hover out, remove class "subhover"
 $(this).removeClass("subhover"); 
 });
 });
</script>
answered May 6, 2011 at 19:06
Sign up to request clarification or add additional context in comments.

2 Comments

Didn't' see your comment \: Can't mark comments as accepted answers, so this question would never get "closed" without an answer \:
no no agreed I wasnt sure about my answer so I commented I am glad that you answered it. I just didn't want to put a half answer in the "answer"

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.