0

I need to add a second level of submenus here.

Is there a dynamic solution, so only one submenu is opened, when clicking on the link? And when clicking a link to a submenu (in the first submenu), then open this one? And so on…

I already changed the jsfiddle a little, to have one level of sublevels more: http://jsfiddle.net/cRsZE/363/

Working example with one level of submenus: JSFiddle Demo

HTML:

<ul id="nav">
    <li>Home</li>
    <li class="parent">About
        <ul class="sub-nav">
            <li>Johnny</li>
            <li>Julie</li>
            <li>Jamie</li>
        </ul>
    </li>
    <li>Contact</li>
</ul>

CSS:

#nav ul.sub-nav {
    display: none;
}

#nav ul.visible {
    display: block;
}

jQuery:

$(document).ready(function() {
    $('.parent').click(function() {
        $('.sub-nav').toggleClass('visible');
    });
});

Source: Creating Drop Down Menu on click CSS

1 Answer 1

0

Try,

CSS:

.hidden {
  display: none;
}

JS:

$('ul ul').addClass('hidden');

$(document).ready(function () {
    $('.parent').click(function (e) {
        e.stopPropagation();
        $(this).find('ul').first().toggleClass('hidden');
    });
});

DEMO

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

5 Comments

Thanks for this! The example works, but I cannot use it this way in my project. Is there a way without adding a class in the beginning before anything happens?
@mandar1ne let me know if the given solution works for you.. :)
"visible" isn't used anywhere, or am I getting this wrong?
@mandar1ne That is unnecessary basically. Is that working in your website.?
No, unfortunately not. At first it works but then it kinda refreshes and is like it was before (so, then menu is "closed" again).

Your Answer

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