here is html:
<ul>
<li class="current"><a href="#">menu item</a></li>
<li><a href="#aba">menu item</a></li>
<li><a href="#abb">menu item</a></li>
<li><a href="#abc">menu item</a></li>
<li><a href="#abd">menu item</a></li>
</ul>
if I press 'a' link it will addClass 'current' to clicked li and remove old li class? this is my try:
$('ul li a').click(function(){
$('ul li').removeClass('current');
... don't know here (add class to current li) ...
});
The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.
on('click','li',function(){ $(this). addClass("active"); // adding active class }); The above code will add the active class to the li tag whenever the user clicks on the Li element.
jQuery toggleClass() Method The toggleClass() method toggles between adding and removing one or more class names from the selected elements.
Use on('click') if you are using latest version of jquery
$('ul li a').on('click', function(){
$(this).parent().addClass('current').siblings().removeClass('current');
});
A fiddle is here.
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