How can make it make
1- open toggle menu when on click menu (not opened by default)
2- close itself – if click other parent menu
trying to make it work for the project
and i copied from cssdesk
$('.inbox li').click(function(e) {
$('.inbox li.active').removeClass('active');
var $this = $(this);
if (!$this.hasClass('active')) {
$this.addClass('active');
}
e.preventDefault();
});
$(document).ready(function () {
$('.tree-toggler').click(function () {
$(this).parent().children('ul.tree').toggle(300);
});
});
<link class="cssdeck" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" class="cssdeck">
<ul class="inbox-nav nav" style="border-top: 1px solid #eef1f5; margin-top:10px;" >
<li class="">
<a href="javascript:;" id="fldr3">menu 1
</li>
<li class="">
<a data-title="Inbox" data-type="important" href="javascript:;">2nd Menu</a>
</li>
<li class="">
<a data-title="Sent" data-type="sent" href="javascript:;">menu2</a>
</li>
<li class="divider"></li>
<li class="active tree-toggler">
<a href="javascript:;" id="fldr2">Toggle menu</a></li>
<ul class="nav nav-list tree">
<li class=""><a href="#">Link</a></li>
<li class=""><a href="#">Link</a></li>
</ul>
</li>
</ul>
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
2
Answers
If user click is not neccesary you can do it by CSS, this is an example the 4th menu item has sub menu items
In your
$('.inbox li').click()
function use jquery’s.hide()
function on theul.tree
element. Since.hide()
won’t do anything if the element is already hidden, you don’t need to check if it needs to be hidden.(Similarly, you can pull the
$this.addClass('active');
statement out of theif
, since it simply won’t do anything if the class is already added!)