Luke Luke - 1 month ago 4
jQuery Question

Third level on Jquery Accordeon Menu

I wonder if it is possible to make the following menu work with three page levels (one in adddition). Here I have to say some words because otherwise I am not allowed to post this question here, Stack O says "It looks like your post is mostly code; please add some more details. Okay, here we go:



$(document).ready(function () {
$('#nav > li > a').click(function(){
if ($(this).attr('class') != 'active'){
$('#nav li ul').slideUp();
$(this).next().slideToggle();
$('#nav li a').removeClass('active');
$(this).addClass('active');
}
});
if($(".active").parent().parent().is('#nav')){
$(".active").next().slideToggle();
}else{
$(".active").parents('ul').siblings('a').click();
}

});

#nav {
float: left;
width: 280px;
}
#nav li a {
display: inline-block;
padding: 10px 15px;
text-decoration: none;
color: #000;
list-style:none;
}
#nav li a:hover, #nav li a.active {
}
#nav li ul {
display: none; // used to hide sub-menus
list-style:none;
}
#nav li ul li a {
padding: 10px 25px;
}
ul{
list-style:none;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="nav">
<li><a href="http://stackoverflow.com/questions/ask" class="active" target="_blank">Item 1</a>
<ul>
<li><a href="#" >Sub-Item 1 a</a></li>
<li><a href="#">Sub-Item 1 b</a></li>
<li><a href="#">Sub-Item 1 c</a></li>
</ul>
</li>
<li><a href="#">Item 2</a>
<ul>
<li><a href="#">Sub-Item 2 a</a></li>
<li><a href="#">Sub-Item 2 b</a></li>
</ul>
</li>
<li><a href="#">Item 3</a>
<ul>
<li><a href="#">Sub-Item 3 a</a></li>
<li><a href="#">Sub-Item 3 b</a></li>
<li><a href="#">Sub-Item 3 c</a></li>
<li><a href="#">Sub-Item 3 d</a></li>
</ul>
</li>
<li><a href="#">Item 4</a>
<ul>
<li><a href="#">Sub-Item 4 a</a></li>
<li><a href="#">Sub-Item 4 b</a></li>
<li><a href="#">Sub-Item 4 c</a></li>
</ul>
</li>
</ul>





https://jsfiddle.net/gcknk4sy/

Thank you for any hints how to archeive this goal.

Answer

$(document).ready(function () {
  $('#nav > li > a').click(function(){
    if ($(this).attr('class') != 'active'){
      $('#nav li ul').slideUp();
      $(this).next().slideToggle();
      $('#nav li a').removeClass('active');
      $(this).addClass('active');
    }
  });
  if($(".active").parent().parent().is('#nav')){
  		$(".active").next().slideToggle();
  }else{
  	$(".active").parents('ul').siblings('a').click();
  }
 
});
#nav {
    float: left;
    width: 280px;
}
#nav li a {
    display: inline-block;
    padding: 10px 15px;
    text-decoration: none;
    color: #000;
    list-style:none;
}
#nav li a:hover, #nav li a.active {
}
#nav li ul {
    display: none; // used to hide sub-menus
     list-style:none;
}
#nav li ul li a {
    padding: 10px 25px;
}

#nav li.sub {
  padding-left: 35px;
}

ul{
  list-style:none;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="nav">
  <li><a href="http://stackoverflow.com/questions/ask" class="active" target="_blank">Item 1</a>
    <ul>
      <li><a href="#" >Sub-Item 1 a</a></li>
      <li><a href="#">Sub-Item 1 b</a></li>
      <li><a href="#">Sub-Item 1 c</a></li>
      <li class="sub"><a href="#" >Sub-sub-Item 1 a</a></li>
      <li class="sub"><a href="#" >Sub-sub-Item 2 a</a></li>
    </ul>
  </li>
  <li><a href="#">Item 2</a>
    <ul>
      <li><a href="#">Sub-Item 2 a</a></li>
      <li><a href="#">Sub-Item 2 b</a></li>
    </ul>
  </li>
  <li><a href="#">Item 3</a>
    <ul>
      <li><a href="#">Sub-Item 3 a</a></li>
      <li><a href="#">Sub-Item 3 b</a></li>
      <li><a href="#">Sub-Item 3 c</a></li>
      <li><a href="#">Sub-Item 3 d</a></li>
    </ul>
  </li>
  <li><a href="#">Item 4</a>
    <ul>
      <li><a href="#">Sub-Item 4 a</a></li>
      <li><a href="#">Sub-Item 4 b</a></li>
      <li><a href="#">Sub-Item 4 c</a></li>
    </ul>
  </li>
</ul>

Comments