Active state management
<ul> <li><a href="#">item 1</a></li> <li><a href="#">item 2</a></li> <li><a href="#">item 3</a></li> </ul> In a click, I would like to add addclass active to the parent li element, and also remove the active class from any other element that may already be active.
+6
3zzy
source share2 answers
$("li a").click( function() { $(".active").removeClass("active"); $(this).parent("li").addClass("active"); }); +6
Tomalak
source share $('div.filter').delegate('a', 'click', function (event) { var theLi = $(this).closest('li'); theLi.siblings('.active:first').removeClass('active'); theLi.addClass('active'); $('ul.items li').hide().filter('.' + this.href.slice(this.href.indexOf("#") + 1)).show(); event.preventDefault(); }); +2
Matt
source share