javascriptjqueryhtmlslidedownslideup

slideUp() slideDown() doesn't work properly


$(document).ready(function(){
  $('.outer').click(function () {

    $('.inner').slideUp();
    /*alert("I BIMS");*/
  });
});
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

html, body {
  background: #0b2027;
  /*line-height: 18px;*/
  height: 100%;
  font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
}

a {
  text-decoration: none;
  color: #fff;
}

.btn-menu {
  top: 51px;
  display: block;
  padding: 20px;
  background: #23282d;
}

.btn-menu .icon {
  float: right;
}

.verticalMenu {
  padding-top: 40px;
  width: 20%;
  min-width: 300px;
  height: 100%;
  display: inline-block;
  /*line-height: 18px;*/
  background: #23282d;
}

.verticalMenu .menu {
  width: 100%;	
  height: 100%;
}

.verticalMenu ul{
  list-style: none;
}

.verticalMenu .menu li a{
  color: #fff;
  display: block;
  padding: 15px 20px;
}

.verticalMenu .menu .outer a:hover{
  background-color: #00b9eb;
  color: #fff;
}

.verticalMenu .menu .inner a:hover{
  color: #00b9eb;
}

.verticalMenu .menu .icon{
  font-size: 12px;
  line-height: 18px;
}

.verticalMenu .menu .icon.left{
  float: left;
  margin-right: 10px;
}

.verticalMenu .menu .icon.right{
  float: right;
  margin-left: 10px;
}

.verticalMenu .menu ul{
  /*display: none;*/
}

.verticalMenu .menu ul li a {
  background: #32373c;
  color: #e9e9e9;
}

.verticalMenu .menu .active > a{
  background: #1a95d5;
  color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="verticalMenu">
  <!--<a href="" class="btn-menu">Menu<i class="icon fa fa-bars"></i></a>-->
  <ul class="menu">
    <li class="outer"><a href=""><i class="icon left fa fa-tachometer-alt"></i> Dashboard</a></li>
    <li class="outer"><a href=""><i class="icon left fa fa-folder"></i> Dokumentverwaltung <i class="icon right fa fa-chevron-down"></i></a></li>
      <ul>
        <li class="inner"><a href=""><i class="icon left fa fa-upload"></i> Upload Datei/Sammlung</a></li>
        <li class="inner"><a href=""><i class="icon left fa fa-plus-circle"></i> Dateil/Sammlung erstellen</a></li>
      </ul>
    <li class="outer"><a href=""><i class="icon left fa fa-key"></i>Rechteverwaltung <i class="icon right fa fa-chevron-down"></i></a></li>
      <ul>
        <li class="inner"><a href=""><i class="icon left fa fa-file"></i> Datei</a></li>
        <li class="inner"><a href=""><i class="icon left fa fa-users"></i> Gruppe</a></li>
        <li class="inner"><a href=""><i class="icon left fa fa-folder-open"></i> Sammlung</a></li>
      </ul>
    <li class="outer"><a href=""><i class="icon left fa fa-user"></i>Userverwaltung <i class="icon right fa fa-chevron-down"></i></a></li>
      <ul>
        <li class="inner"><a href="">User erstellen</a></li>
        <li class="inner"><a href="">User löschen</a></li>
        <li class="inner"><a href="">Gruppe erstellen</a></li>
        <li class="inner"><a href="">Gruppe löschen</a></li>
        <li class="inner"><a href="">User-Gruppe zuordnen</a></li>
        <li class="inner"><a href="">User-Gruppe löschen</a></li>
      </ul>
  </ul>
 </div>

I want a accodion-menu. If the users hits one elment of the "outer"-menu it should succeed as follows:

if(this submenu is closed){
   1. open it
   2. close the other submenu
} else
{
  do nothing
}

It doesn't even react like this. So seems like I'm calling the wrong tag, with the .slideUp() function. Doesn't matter if I use slideUp() or slideDown() - please help :-)


Solution

  • Put this jQuery :

    $(document).ready(function(){
       $('.outer').click(function () {
           $('.inner').slideUp();
           $(this).next().find('.inner').slideDown();
       });
    });
    

    It will slideUp all '.inner' classes and then it will select the good outer with $(this) selector, choose the elements after (I mean the ul), find all '.inner' classes and slide them down.

    Check at this jsfiddle : jsfiddle

    Wish it helped you.

    EDIT : If you want the menu to be wrapped at the beginning, you can make it with Jquery :

    $(document).ready(function(){
       $('.inner').slideUp();
       $('.outer').click(function () {
           $('.inner').slideUp();
           $(this).next().find('.inner').slideDown();
       });
    });
    

    or with CSS :

    .inner{
        display:none;
    }