Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing active state

<ul>
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
</ul>

On click, I'd like to addclass active to the parent li element while also removing the active class from any other element which may be active already.

like image 866
eozzy Avatar asked Apr 24 '10 10:04

eozzy


2 Answers

$("li a").click( function() {
  $(".active").removeClass("active");
  $(this).parent("li").addClass("active");
});
like image 187
Tomalak Avatar answered Sep 21 '22 11:09

Tomalak


$('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();
});
like image 45
Matt Avatar answered Sep 22 '22 11:09

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!