Darku
Darku

Reputation: 97

How to hide one submenu when click on another

I've got simply javascript to toggle submenu. But there's a problem. I just want to see one submenu at once. How can I fix it?

<script type="text/javascript">
$(document).ready(function() {
    $(".nav > li").click(function(){
        $(this).children('.submenu').css("margin-left", "0").toggle();
    });
});
</script>

Upvotes: 0

Views: 100

Answers (2)

prakashapkota
prakashapkota

Reputation: 321

First close all the submenu opened, then show only the submenu of the menu clicked: Like this

$(".nav > li").click(function(){
    ('.submenu').hide();
    $(this).children('.submenu').css("margin-left", "0").show();
});

Upvotes: 0

Andy
Andy

Reputation: 14575

This should work:

$('.submenu').hide();                 // hide all submenu's
$(this).children('.submenu').show();  // show the one you clicked

Upvotes: 2

Related Questions