I am trying to create a menu in which I want to change the CSS of an li element on click while at the same time, CSS of other li's should remain the same.
My menu is:
<ul id="menu">
<li><a href="#">Parent 1</a> </li>
<li><a href="#">item 1</a></li>
<li><a href="#">non-link item</a></li>
<li><a href="#">Parent 2</a> </li>
</ul>
and my jquery to add CSS to the selected element is:
$("#menu li a").click(function() {
$(this).parent().addClass('selected');
});
However, right now, I am unable to remove the added CSS from a non-selected element. Is there anyway I can implement this?
click(function() { $('selector. active'). removeClass("active"); $(this). addClass("active"); }); });
removeClass() Method. This method removes one or more class names from the selected elements. If no parameter is specified in the removeClass() method, it will remove all class names from the selected elements.
Try this
$("#menu li a").click(function() {
$(this).parent().addClass('selected').siblings().removeClass('selected');
});
DEMO
Try this
$("#menu li a").click(function() {
$("#menu li").removeClass('selected');
$(this).parent().addClass('selected');
});
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With