0

In a nested menu, I want to show the submenu of an element when that element is hovered over.

HTML:

<div id="menu">
    <div class="menu-main-menu-container">
        <ul>
            <li id="mojo_page_list_index" class="mojo_active"><a href="http://localhost/index.php">Home</a></li>
            <li id="mojo_page_list_over"><a href="http://localhost/index.php/over">Over</a>
                <ul>
                    <li id="mojo_page_list_bedrijfsprofiel"><a href="http://localhost/index.php/bedrijfsprofiel">Bedrijfsprofiel</a></li>
                    <li id="mojo_page_list_geschiedenis"><a href="http://localhost/index.php/geschiedenis">Geschiedenis</a></li>
                </ul>
            </li>
            <li id="mojo_page_list_contact"><a href="http://localhost/index.php/contact">Contact</a></li>
        </ul>
    </div>
</div>

jQuery:

(function($){   
    $(document).ready(function(){
        $("ul.menu-main-menu-container li").hover(function () {                          
            $(this).addClass("hover");
            $('ul:first', this).css({visibility: "visible",display: "none"}).slideDown(200);
        }, function () {
            $(this).removeClass("hover");
            $('ul:first', this).css({visibility: "hidden"});
        });

        if ( ! ( $.browser.msie && ($.browser.version == 6) ) ){
            $("ul.menu-main-menu-container li ul li:has(ul)").find("a:first").addClass("arrow");
        }               
    });
})(window.jQuery);

What am I doing wrong?

1
  • What do you mean with "doesn't work"? What do you expect, and what is happening actually? Commented Aug 27, 2012 at 11:48

3 Answers 3

1

Something like this:

 <div id="menu">
    <div class="menu-main-menu-container">
    <ul>
        <li id="mojo_page_list_index" class="mojo_active"><a href="http://localhost/index.php">Home</a></li>
        <li id="mojo_page_list_over"><a href="http://localhost/index.php/over">Over</a>
            <ul style="display:none;">
                <li id="mojo_page_list_bedrijfsprofiel"><a href="http://localhost/index.php/bedrijfsprofiel">Bedrijfsprofiel</a></li>
                <li id="mojo_page_list_geschiedenis"><a href="http://localhost/index.php/geschiedenis">Geschiedenis</a></li>
            </ul>
        </li>
        <li id="mojo_page_list_contact"><a href="http://localhost/index.php/contact">Contact</a></li>
    </ul>
    </div>
</div>

   jQuery(function(){ 
       $(".menu-main-menu-container ul li").hover(function () {                          
            $(this).addClass("hover");
            $('ul:first', this).css({display: "none"}).slideDown(200);
        }, function () {
            $(this).removeClass("hover");
            $('ul:first', this).css({display: "hidden"});
        });
    });

Updated Just replace your js script in your ( fiddle http://jsfiddle.net/kL8RF/1/ )

jQuery(function(){ 
   $("#menu li").hover(function () {                          
        $(this).addClass("hover");
        $('ul', this).slideDown(100);  
    }, function () {
        $(this).removeClass("hover");
        $('ul', this).slideUp(100);
    });
});​
Sign up to request clarification or add additional context in comments.

1 Comment

I tried all your options and I got this so far: jsfiddle.net/kL8RF/1 I think there is something wrong with the stylesheet. But it is fun learning this way. Is there somebody who could help me and tell me what I am doing wrong now with this stylesheet?
0

Your selector in the jQuery is wrong. Its looking for class of menu-main-menu-container within a ul and there is not one. I would suggest you debug this in stages, first select the html element you wat and do an alert in jQuery. This will show you have the right element selected then you can go from there with the events on that element.

Comments

0

It was jQuery selectors problem. It is looking for a ul with class main-menu-... but not looking for a ul which is descendant of class main-menu-... This should solve your problem.

Comments

Your Answer

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.