views:

30

answers:

1

HTML:

<ul id="list">
  <li>item</li>
  <li>item</li>
  <li>item</li>
  <li>item</li>
  <li class="action">item</li>
</ul>

jQuery:

$("#list li:last").addClass("active");

This almost works, but the "action" item takes the "active" class when its present which I don't want. How do I rewrite the above jQuery so it always ignores the li.action and assigns "active" class to the last li before it??

Thanks

+3  A: 

Use the :not() selector. E.g.

$("#list li:last:not(.action)").addClass("active");
BalusC
Oh, chaining works there too? Cool!
Nimbuz
jQuery is one and all builder pattern. It is certainly cool.
BalusC
Great, thanks! :)
Nimbuz