+ Reply to Thread
Results 1 to 9 of 9

Thread: jQuery hover menu - Nothing works

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    jQuery hover menu - Nothing works

    I am working on a hover menu using jQuery. For some reason nothing seems to work. What I want it to do is when you hover over the anchor tag the ol that is after it will expand revealing the contents of that subject header and retract when it is mousedout. I have no idea what is wrong.

    JQuery:
    Code:
    $(".HM_menuItem ol").hide();
    $(".HM_menuItem a").hover(
    	function() {
    		$(this).find("ol").slideToggle();
    	},
    	function() { 
    		$(this).find("ol").slideToggle();
    	}
    );
    HTML:
    HTML Code:
    		<div id="navi-menu">
    			<div class="HM_menuItem">
    				<a href="#">Root 1</a>
    				<ol>
    					<li>Child 1</li>
    					<li>Child 2</li>
    				</ol>
    			</div>
    			<div class="HM_menuItem">
    				<a href="#">Root 2</a>
    				<ol>
    					<li>Child 1</li>
    					<li>Child 2</li>
    				</ol>
    			</div>
    		</div>
    Here is a link to a live site.

    Thanks
    Last edited by as4s1n; 06-13-2010 at 12:41 AM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: jQuery hover menu - Nothing works

    Firstly, the sample page calls the jquery methods before the elements exist. Use ready() to set up the handlers and hide the elements (you should have caught this one).

    Second, find() searches descendants. Use next() or nextAll() to get following siblings.
    Last edited by misson; 06-13-2010 at 02:14 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: jQuery hover menu - Nothing works

    In other words:

    Code:
    $(document).ready(function(){
    	$(".HM_menuItem ol").hide();
    	$(".HM_menuItem a").hover(
    		function() {
    			$(this).next("ol").slideToggle();
    		},
    		function() { 
    			$(this).next("ol").slideToggle();
    		}
    	);
    });
    That should do the trick ;)

  4. #4
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: jQuery hover menu - Nothing works

    Oh yea, I completely blanked about that $(document).ready(function() {});. The reason I was using $.find() was because I was looking on Dark Horizons they have a menu with code like this:

    Code:
    $("#content-navigation .navigation-tongue").hover(
        function () {
            $(this).find("ol").toggle("fast");
            $(this).addClass("selected");
        },
        function () {
            $(this).find("ol").toggle("slow");
            $(this).removeClass("selected");
        }
    )
    With a menu structure that looks like:

    HTML Code:
    <div id="content-navigation">
    	<ul>
    		<li id="content-navigation-news" class="navigation-tongue">
    			<a href="/news/">News</a>
    			<ol>
    				<li><a href="/news/">Latest News</a></li>
    				<li><a href="/news/archive/">News Archive</a></li>
    
    			</ol>
    		</li>
    		<li id="content-navigation-interviews" class="navigation-tongue">
    			<a href="/interviews/">Interviews</a>
    			<ol>
    				<li><a href="/interviews/">Latest Interviews</a></li>
    				<li><a href="/interviews/a/">Interviews Archive</a></li>
    
    			</ol>
    		</li>
    <!-- Etc -->
    I used a similar structure but then why does that work when mine doesn't?

    Also: Whenever you mouseout of the anchor tag the OL retracts, I want people to be able to hover over that while the menu section is expanded so it would be possible to click the links on that. Any help with that?
    Last edited by as4s1n; 06-13-2010 at 12:56 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: jQuery hover menu - Nothing works

    Quote Originally Posted by as4s1n View Post
    I used a similar structure but then why does that work when mine doesn't?
    Similar isn't the same as same. Compare:
    Code:
    $("#content-navigation .navigation-tongue")[...]
    HTML Code:
    <div id="content-navigation">
      <ul>
        <li id="content-navigation-news" class="navigation-tongue">
          <a href="/news/">News</a>
          <ol>
    with:
    Code:
    $(".HM_menuItem a")[...]
    HTML Code:
    <div id="navi-menu">
      <div class="HM_menuItem">
        <a href="#">Root 1</a>
        <ol>
    Use of lists for the top level menu aside (which is more semantic, hence preferable), what's the main difference?

    Quote Originally Posted by as4s1n View Post
    Also: Whenever you mouseout of the anchor tag the OL retracts, I want people to be able to hover over that while the menu section is expanded so it would be possible to click the links on that. Any help with that?
    Once you spot the difference, you'll see why yours behaves the way it does and how to get the other behavior.
    Last edited by misson; 06-13-2010 at 02:11 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: jQuery hover menu - Nothing works

    I'm sorry, I am still in the dark with that. I just can't seem to figure it out. I worked on something a year ago and dropped it because I never understood this. My guess would be something along these lines:

    Code:
    // ...
    $("#navi-menu .HM_menuItem a").hover(
        function() {
            $(this).next("ol").slideDown();
                if($(this).is(":ol")) {
                    $(this).hover(
                    function() {
                            $(this).show();
                        },
                        function() {
                            $(this).slideUp();
                        });
                 }
        }
    });
    But that's probably wrong.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: jQuery hover menu - Nothing works

    Take a close look at the selectors and structure. The Dark Horizons menu selects the parent .navigation-tongue ($("#content-navigation .navigation-tongue")):
    HTML Code:
    <div id="content-navigation">
      <ul>
    ---><li id="content-navigation-news" class="navigation-tongue">
          <a href="/news/">News</a>
          <ol>
    whereas yours selects the sibling link ($(".HM_menuItem a")):
    HTML Code:
    <div id="navi-menu">
      <div class="HM_menuItem">
    ---><a href="#">Root 1</a>
        <ol>
    Thus the Dark Horizons menu can use find(), since the <ol> is a child of their selected element, but you must use next(), since the <ol> is a sibling of your selected element.

    A consequence of the different selectors is that when the mouse is hovering over the <ol> it's still hovering over Dark Horizons' .navigation-tongue, keeping the <ol> open. In your menu, hovering over the <ol> requires leaving the <a>, which causes the menu to close.
    Last edited by misson; 06-13-2010 at 05:22 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: jQuery hover menu - Nothing works

    Thanks Misson, that makes so much more sense now.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  9. #9
    Join Date
    Jun 2010
    Posts
    1

    Re: jQuery hover menu - Nothing works

    thanks

+ Reply to Thread

Similar Threads

  1. JQuery sliding menu - Remember which link was clicked
    By as4s1n in forum Programming Help
    Replies: 11
    Last Post: 05-25-2010, 07:06 PM
  2. How to make realistic hover with js?
    By Teensweb in forum Programming Help
    Replies: 0
    Last Post: 08-25-2008, 03:16 AM
  3. Replies: 2
    Last Post: 01-25-2008, 09:09 AM
  4. Popup Menu Script Works on old host, but not x10?
    By GG-Xtreme in forum Free Hosting
    Replies: 10
    Last Post: 08-25-2007, 07:45 AM
  5. [ayuda] Css Hover
    By medina in forum General
    Replies: 11
    Last Post: 04-27-2007, 05:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers