1
I want to add a class hidden
for all li
, but not for the li
that was clicked. How can I do this using jQuery?
jQuery(".render-menu li").on('click', function() {
alert();
jQuery(".render-menu").not($(this)).parent().addClass('hidden');
});
.hidden {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="render-menu">
<li class="font-size">list 1</li>
<li class="font-size">list 2</li>
<li class="font-size">list 3</li>
<li class="font-size">list 4</li>
</ul>
It was typo, I’ve made the correction.
– user125347
It is interesting to note that to apply the class you did not select the elements
<li>
, only the class.render-menu
, which is the<ul>
; then did the not with thethis
, that will do nothing, because thethis
will be the<li>
pressed; then takes the parent element, which will be the parent element of the<ul>
, in the example will be the<body>
whole and finally you add the classhidden
. Instead of hiding the<li>
you have hidden page :D– Woss