0

I developed the following code for a simple menu:

$(document).ready(function () {
    $(".main_menu_01, .main_menu_02").on('click', function () {
    
    var $panel = $(this).next('.panel');
    if ($panel.is(':visible')) {
      $panel.add($panel.find('.panel')).slideUp(500).removeClass('active');
    } else {
      $panel.slideDown(500).addClass('active');
    }
    
    });
});
.panel{ 
 width: 100%;
 padding-left: 0%;
 font-weight: bold;
 overflow: hidden;
 display:none;
}

.main_menu_01 {
 padding-left: 1%;
 background: blue;
}

.main_menu_02 {
 padding-left: 5%;
 background: lime;
}

.sub_menu_01{
 padding-left: 1%;
 background: lime;
}

.sub_menu_02{
 padding-left: 10%;
 background: lime;
}


.main_menu_01:before, .main_menu_02:before {
 content:'+';
 width:20px;
 display:inline-block;
}

.main_menu_01.active:before, .main_menu_02.active:before {
 content:'-';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li class="main_menu_01"> 1.0 Main Menu </li>
  <ul class="panel">
        
    <li class="sub_menu_01"> 1.1 Sub Menu </li>
     
    <li class="main_menu_02"> 1.2 Sub Menu </li>
      <ul class="panel">
  
      <li class="sub_menu_02"> <a> 1.2.1 Sub Menu </a> </li>
      <li class="sub_menu_02"> <a> 1.2.2 Sub Menu </a> </li> 

      </ul> 
   
  </ul>

All this works fine so far. You can also find the code in the JSFiddle here.


As you can see I want to add a + and - sign to the main_menu buttons depending if the menu is opened or not. Therefore, I tried to go with the before code in the CSS. However, currently the + sign remains although the menu is opened.

I am not sure if this issue might be caused by the jQuery code. However, I need this jQuery code to close/open the entire menu in case the button main_menue_01 is clicked.

Do you have any idea what I need to change in my code so I can have the full function provided by the jQuery code and a +/- sign?

Michi
  • 4,663
  • 6
  • 33
  • 83
  • If you look at the changing class attributes in your browser’s inspector, you’ll notice that the element which receives the `active` class (`
      `) is not the same as the element you’ve attached your `+`/`-` to (`
    • `).
    – MTCoster Nov 15 '18 at 17:43
  • Make separate classes for active/inactive which handles the +/- and add/remove those classes in your if. – nurdyguy Nov 15 '18 at 17:56

1 Answers1

0

You need to set the active class on the banner, i.e. where the click happens. From there you can change the style with respect to state, e.g.

$(document).ready(function () {
    $(".main_menu_01, .main_menu_02").on('click', function () {
      var $panel = $(this).next('.panel');
      if ($panel.is(':visible')) {
        $panel.add($panel.find('.panel')).slideUp(500);
        $(this).hasClass('main_menu_01') ? $('.menu').removeClass('active') : $(this).removeClass('active');
      } else {
        $panel.slideDown(500);
        $(this).addClass('active');
      }
    });
});
.panel{ 
 width: 100%;
 padding-left: 0%;
 font-weight: bold;
 overflow: hidden;
 display:none;
}

.main_menu_01 {
 padding-left: 1%;
 background: blue;
}

.main_menu_02 {
 padding-left: 5%;
 background: lime;
}

.sub_menu_01{
 padding-left: 1%;
 background: lime;
}

.sub_menu_02{
 padding-left: 10%;
 background: lime;
}

 .main_menu_01:before, .main_menu_02:before {
 content:'+';
 width:20px;
 display:inline-block;
}

.main_menu_01.active:before, .main_menu_02.active:before {
 content:'-';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li class="menu main_menu_01"> 1.0 Main Menu </li>
  <ul class="panel">
        
    <li class="sub_menu_01"> 1.1 Sub Menu </li>
     
    <li class="menu main_menu_02"> 1.2 Sub Menu </li>
      <ul class="panel">
        <li class="sub_menu_02"> <a> 1.2.1 Sub Menu </a> </li>
        <li class="sub_menu_02"> <a> 1.2.2 Sub Menu </a> </li> 
      </ul> 
  </ul>
onyx
  • 1,588
  • 7
  • 14
  • Hi onyx, thanks for your answer but it only works partly. If you open main_menu_01 and main_menu_02 and after that you close all menus by clicking on main_menu_01 and then you open the menu again by clicking on main_menu_01 the main_menu_02 will have a - sign but it should be a + sign. – Michi Nov 15 '18 at 18:39
  • @Michi, I updated so if you close the `main_menu_01` it won't close the inner one. – onyx Nov 15 '18 at 18:53
  • Sorry if my previous comment was not so clear but the functionality of closing and opening the menu worked exactly the way I wanted it before. Only the +/- sign did not work properly. What I need is: If you close the main_menu_01 I want that that the inner one is closed as well AND that the + sign of the inner one changes to a - sign. – Michi Nov 15 '18 at 20:10
  • Nice. Exactly how I need. Would be cool if you upvote my question. – Michi Nov 16 '18 at 07:27
  • I also updated the JSfiddle here: https://jsfiddle.net/ogbn8x6v/3/. Guide about using multiple classes can be found here: https://stackoverflow.com/questions/17366432/multiple-classes-on-single-element-html – Michi Nov 16 '18 at 07:44