I would like the children to be hidden when parent is clicked again but not when children are clicked. For some reason children are treated as $level1 elements while I was expecting them not to respond to $level1.click function. I think I'm missing something trivial, but I can't help myself. Here you are the filddle https://jsfiddle.net/bxmbtzrb/3 Thank you for your suggestions.
The structure
<nav>
<ul>
<li>
<a>PARENT</a>
<ul>
<li>
<a href="#">CHILD 1</a>
</li>
<li>
<a href="#">CHILD 2</a>
</li>
<li>
<a href="#">CHILD 3</a>
</li>
</ul>
</li>
</ul>
The Jquery script:
var $level1 =$('nav').children('ul').children('li');
$level1.click(function() {
if ($(this).hasClass('on')) {
$(this).removeClass('on');
} else {
$level1.filter('.on').removeClass('on');
$(this).addClass('on');
}
});
nav li:not(.on) li {
display: none;
}
and CSS:
nav li:not(.on) li {
display: none;
}