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?
`) is not the same as the element you’ve attached your `+`/`-` to (`- `).
– MTCoster Nov 15 '18 at 17:43