I have a nav menu that needs to trigger with clicks rather than hovers. When the links are clicked, an .open
class would be added to the parent li
. If that parent already has the .open
class, then it would get removed. It would also be removed if another link is clicked on. So far I can get the class added when clicked and removed when a sibling is clicked, but not removed when it’s already .open
.
I tried adding a hasClass
conditional, but that didn’t work either. Seemed like it reruns the function every time it’s clicked and therefore ignores the hasClass conditional.
Can anyone provide help? I tried toggleClass
, but that didn’t work.
$('li a').on('click', function() {
$('li a').parent().removeClass('open');
$(this).parent().addClass('open');
});
ul {
list-style: none;
}
li {
display: inline-block;
padding: 10px;
}
.open {
background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
<ul>
<li>
<a href="#">Item 1</a>
</li>
<li>
<a href="#">Item 1</a>
</li>
</ul>
</nav>
4
Answers
So you only want the yellow background to appear as a signifier of user interaction rather than for the background color to be displayed? Have you tried using the mousedown/mouseup functions instead of
.on('click', function(){...}
?I was able to simulate the click event where the color showcases via this method:
To do what you require you can use
toggleClass()
on the parentli
when the element is clicked. To remove the class from all otherli
elements you can useremoveClass()
along withnot()
to exclude the currentli
. Try this:You can use
Below is the link for the demo
https://jsfiddle.net/so1u8hq6/
Late to the party, but, after seeing the provided answers and some of the CSS you use I had to urge with my suggestions:
display
and move on. Style directly thea
tag (with the necessary paddings etc.). You’ll not only get less CSS to take care of, but also a larger touch interaction area. Makes no sense to style something yellow if it’s not a UI part of something interactable. Also in JS, you don’t need to take care about theLI
wrappers any more – but only about the actualA
Elements.$('li a')
– those might target any LI→A elements in your app. Instead be more specific and use a Class like i.e:.tabs
for the parentUL
. Both in CSS and JS..on()
method). Not only it will help you to catch theEvent.delegateTarget
parentUL
where needed, but also thethis
(the clicked element), but mainly reference all the "group" ofa
elements enclosed in the common parent. That way you can have as many.tabs
in a single page as you like. And yes, thanks to Event delegation you can even add dynamically LI Elements – and your JS will still work as expected.<a href="#">
Anchor elements, instead of (more properly)<button type="button>"
Elements, you need to also useEvent.preventDefault()
in order to prevent the browser its default behavior and that’s to follow anchors (scroll the page, navigate, etc…)"a.open"
when you want to target and remove the"open"
class. By just using"a"
(or in other answers on this page –"li"
) you’re uselessly touching elements trying to remove a class that’s not there in the first place.Finally, here’s the CSS retouch and the proper jQuery needed for your task:
To explain the only complicated line:
the rest is pretty self explanatory.
Further read: