i have any navigation menu !
<div class="nav">
<ul class="navigation">
<li class="selected"><a href="#">HOME</a></li>
<li><a href="#">PROFILE></a></li>
<li><a href="#">CONTACTUS</a></li>
<li><a href="#">ABOATUS</a></li>
</ul>
</div>
Now how to add selected class after click to any ( profile, Contactus, aboutus ) and remove selected home using Jquery.
Thanks
On the assumption that you want to have only one element of class-name selected
I suggest:
$('ul.navigation li a').click(
function(e) {
e.preventDefault(); // prevent the default action
e.stopPropagation(); // stop the click from bubbling
$(this).closest('ul').find('.selected').removeClass('selected');
$(this).parent().addClass('selected');
});
JS Fiddle demo.
you can use toggleClass:
$("#myElement1").click(function(){
$("#myElement2").toggleClass("myClass");
});
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