I have 4 anchors, I want to add a class of current to an anchor as it is clicked and remove the class from the other 3 at the same time. Here\'s my code. what am I doing wro
I think this will help you
$("a").click(function() {
$('a').removeClass('current');
var id = $(this).attr('id');
$('#'+id).addClass('current');
});
By using event-delegation, we only bind one handler for all click events. When an anchor (other than the .current
anchor) is clicked, we strip the .current
anchor of its class, and make the clicked anchor the new current one:
$("#thumbs").on("click", "a:not(.current)", function ( event ) {
$(".current", event.delegateTarget).removeClass("current");
$(this).addClass("current");
});