I need need a way to get my active state to work properly in my sidebar.
Currently my problems are:
-
When a li is clicked, all children (.sub-menu) receive the active state state.
-
When a li (not sub-menu) is clicked after having clicked a submenu li the active state of the clicked submenu li is not removed.
Hope you can help.
$("#sidebar li").click(function(){
$(this).siblings().removeClass('active');
$(this).addClass('active');
$('#sidebar li').children().removeClass('active');
});
html
<div class="col-md-4" id="sidebar">
<?php
if (is_page('gulvservice')){
wp_nav_menu(array('menu'=>'gulvservice' ));
} elseif (is_page('malerservice')) {
wp_nav_menu(array('menu'=>'malerservice' ));
} elseif (is_page('industrilakering')) {
wp_nav_menu(array('menu'=>'industrilakering' ));
}
?>
<!-- <?php wp_nav_menu( array( 'theme_location' => 'gulvservice' ) ); ?> -->
<div class="gulvservice-kontaktinfo">
<p>EM. Meyers Eftf. A/S</p>
<p>CVR: 82510028</p>
<p>Tlf.: 36349500 / 40384053</p>
<p>E-mail: gulve@meyers.dk</p>
</div>
</div>
rendered html:
Found solutions to both problems.
1:
I achieved this using pseudo elements through css instead of jquery.
The reason i made some :before elements was due to some indentation on my sub-menu list items.
CSS:
2: Credit goes to @mark.hch
First remove active class from all list items.
Next add active class to the clicked list item.