Skip to content

Instantly share code, notes, and snippets.

@sarvar
Created October 3, 2019 06:01
Show Gist options
  • Save sarvar/f04d9de68930411003ebad2c0dae2924 to your computer and use it in GitHub Desktop.
Save sarvar/f04d9de68930411003ebad2c0dae2924 to your computer and use it in GitHub Desktop.
add class on click and remove from others
<ul>
<li><a id="about-link" class="" href="#">ABOUT</a></li>
<li><a id="events-link" href="#">EVENTS</a></li>
<li><a id="reviews-link" href="#">REVIEWS</a></li>
<li><a id="contact-link" href="#">CONTACT</a></li>
</ul>
$('ul li a').click( function(){
if ( $(this).hasClass('current') ) {
$(this).removeClass('current');
} else {
$('li a.current').removeClass('current');
$(this).addClass('current');
}
});
a {
transition:all 200ms;
color:#333;
background:#eaeaea;
}
.current {
color:#900;
background:#bbb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment