0

I want to make a submenu with jQuery and javascript. I have this html structure:

<nav class="container">
    <ul>
        <li><a href="#" title="Werken in de zorg">Werken in de zorg</a>
            <ul class="submenu">
                <li><a class="label" href="#" title="Lees meer over Werkvelden">Werkvelden</a></li>
                <li><a href="#" title="Lees meer over Eerstelijnzorg">Eerstelijnzorg</a></li>
                <li><a href="#" title="Lees meer over Geestelijke Gezondheidszorg">Geestelijke Gezondheidszorg</a></li>
                <li><a href="#" title="Lees meer over Gehandicaptenzorg">Gehandicaptenzorg</a></li>
            </ul>
        </li>
        <li><a href="#" title="Vacatures">Vacatures</a></li>
        <li><a href="#" title="Opleidingen en scholen">Opleidingen en scholen</a></li>
    </ul>
</nav>

In the CSS is set the .submenu to display none. But now the javascript. I would like. If you have a hovert li item. Then the submenu appear. And when you go from the submenu. Then that still remain 300ms. But how should I do with jQuery?

Thank you very much for your help!

2
  • 2
    There are so many ready-made dropdown menu solutions out there that do this. Best check out one of them Commented Dec 21, 2011 at 12:33
  • Do you have what? What are good? Commented Dec 21, 2011 at 12:42

2 Answers 2

1

You can give this a try:

$(document).ready(function() {
    $('nav.container').children('ul').children('li').hover(function() {
        if ($(this).find('.submenu').length > 0) {
            $(this).find('.submenu').slideToggle();
        }
    }, function() {
        $(this).find('.submenu').delay(300).slideToggle();
    });
});

Here's a fiddle to show it working.

Sign up to request clarification or add additional context in comments.

2 Comments

This only seems to work on the very first li with a ul. If there are other submenu blocks they don't open.
I also get this error when I include this jquery in my page: Uncaught SyntaxError: Unexpected token ILLEGAL
0

try superfish http://users.tpg.com.au/j_birch/plugins/superfish/#examples

it has a delay and you can use hoverintent

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.