I'm currently working on a sidebar menu that involves a lot of javascript. The challenge I am facing is with the submenu items within some of the menu items. When I click on a menu item, its submenu opens using jQuery slideToggle, but when I click on another menu item, the submenus also toggle open. I want to make it so that only one submenu is open at a time.
Apologies if my description is confusing, but I hope you get the gist of what I'm trying to achieve.
You can view the code on jsFiddle
<script>
$(document).ready(function(){
var guts = $('#guts').css('display');
var guts2 = $('#guts2').css('display');
var guts3 = $('#guts3').css('display');
$("#click").click(function(){
$("#guts").slideToggle("fast");
if (guts2 == 'block')
$("#guts2").slideToggle("fast");
if (guts3 == 'block')
$("#guts3").slideToggle("fast");
$(this).addClass("active").siblings('.active').removeClass('active');
});
$("#click2").click(function(){
$("#guts2").slideToggle("fast");
if (guts == 'block')
$("#guts").slideToggle("fast");
if (guts3 == 'block')
$("#guts3").slideToggle("fast");
$(this).addClass("active").siblings('.active').removeClass('active');
});
$("#click3").click(function(){
$("#guts3").slideToggle("fast");
if (guts2 == 'block')
$("#guts2").slideToggle("fast");
if (guts == 'block')
$("#guts").slideToggle("fast");
$(this).addClass("active").siblings('.active').removeClass('active');
});
$("#home").click(function(){
$(this).addClass("active").siblings('.active').removeClass('active');
if (guts == 'block')
$("#guts").slideToggle("fast");
if (guts2 == 'block')
$("#guts2").slideToggle("fast");
if (guts3 == 'block')
$("#guts3").slideToggle("fast");
});
});
</script>
<div id="links">
<a href="#/Home" id="home" class="active">Home</a>
<a href="#/Staff" id="click">Staff</a>
<div id="guts">
<a href="#" class="guts">• Staff List</a>
</div>
<a href="#/Locations" id="click2">Locations</a>
<div id="guts2">
<a href="#" class="guts">• Location List</a>
</div>
<a href="#/Calendar" id="click3">Calendar</a>
</div>