I've got the following HTML:
<a href="#">img/a.jpg</a>
<a href="#">img/b.jpg</a>
<a href="#">img/c.jpg</a>
For selected links I've got the following:
$(function() {
$("a").click( function( )
{
var current = $(this);
var name = current.attr('href');
current.addClass("selected");
var prev = current.closest("a").find("a.selected");
prev.removeClass("selected");
return false;
});
});
The problem is that after I click some link the previous one I clicked won't deselect(remove class selected). Any help will be greatly appreciated.
You could remove all elements that have a class of selected first, then apply your selected class to the current one.
$("a").click(function( {
var current = $(this);
var name = current.attr('href');
//Remove all
$('.selected').removeClass('selected');
//Add to current
current.addClass("selected");
return false;
});
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