add/remove active class for ul list with jquery?


I'm trying to remove and set an active class for a list item every time it's clicked. It's currently removing the selected active class but isn't setting it. Any idea what I'm missing?


<ul class="nav nav-list">
  <li class='nav-header'>Test</li>
  <li class="active"><a href="page1.php">Page 1</a></li>
  <li><a href="page2.php">Page 2</a></li>
  <li><a href="page3.php">Page 3</li>


 $('.nav-list').click(function() {


This question is tagged with javascript jquery html

~ Asked on 2013-11-24 18:50:42

The Best Answer is


this will point to the <ul> selected by .nav-list. You can use delegation instead!

$('.nav-list').on('click', 'li', function() {

~ Answered on 2013-11-24 18:53:33


you can use siblings and removeClass method

$('.nav-link li').click(function() {

~ Answered on 2014-05-28 13:31:20

Most Viewed Questions: