tags:

views:

63

answers:

6

I have a list of links, and I would like to use jQuery to set the clicked one as active, and have the rest of them remove their class.

My current setup is like this:

html

<ul>
  <li id="li_1" class="active"><a href="#" id="link1">link 1</a></li>
  <li id="li_2"><a href="#" id="link2">link 2</a></li>
  <li id="li_3"><a href="#" id="link3">link 3</a></li>
</ul>

jquery

$("#link1").click(function () {
  $("#li_1").removeClass('active');
  $("#li_2").removeClass('active');
  $("#li_3").addClass('active');
});
$("#link2").click(function () {
  $("#li_1").addClass('active');
  $("#li_2").removeClass('active');
  $("#li_3").removeClass('active');
});
$("#link3").click(function () {
  $("#li_1").removeClass('active');
  $("#li_2").addClass('active');
  $("#li_3").removeClass('active');
});

Obviously this isn't pretty in the least, and I'd like to have it become an extremely simple and flexible 1-2 liner function. I know this is possible, but unfortunately I don't possess the jQuery-fu that I know many of you do :)

+4  A: 

You'll need a way to find the links, for example a class or an ID for the <ul>, so you'll be able to do

$(".theclass").click(function() {
    $(this).addClass('active').siblings().removeClass('active');
});

(the example above should work if all the li's have the class theclass; you don't need an ID or class for the <a>s)

MvanGeest
Aww you beat me by like, 30 seconds.
cdutson
worked perfectly...thanks!
espais
sorry, i liked patrick's selector suggestion better :)
espais
Yeah, I should have extended my answer with multiple solutions. Nevertheless, my answer will be here for others to see and learn from, so it's OK.
MvanGeest
+5  A: 
$('ul > li > a').click(function() {
    $(this).parent().addClass('active').siblings().removeClass('active');
    return false;
});

Or perhaps better would be to place an ID on the <ul> :

$('#myUL > li > a').click(function() {
    $(this).parent().addClass('active').siblings().removeClass('active');
    return false;
});
patrick dw
adds the class properly but doesn't remove the existing class
espais
Was there a `.siblings()` overload added I'm not aware of? :)
Nick Craver
@espais - Sorry, typo. Answer has been updated. :o)
patrick dw
@patrick: another typo, `s/'removeClass'/'active'
jAndy
your second edit is exactly what i was looking for
espais
@jAndy, @Nick - Rough morning. :o)
patrick dw
+1  A: 

use .toggleClass( className ) : - http://api.jquery.com/toggleClass/

Example :

$('#foo').toggleClass(className, addOrRemove);
Pranay Rana
+1  A: 
/* generic selector, you'd want to give the ul an id or something)*/

    $("ul li a").click(function () {
       $(this).closest('li').addClass('active').siblings().removeClass('active');
    }
cdutson
You would want to use `.closest()` instead of `.parents()` here, in a nested `<ul>` situation `.parents()` might have undesirable side-effects :)
Nick Craver
you are absolutely correct. I've edited my suggestion based on that.
cdutson
+1  A: 

Try (sorry for edits kept getting syntax wrong):

$("li").click(function()
{ 
    $("li").removeClass('active');
    $(this).closest("li").addClass('active');
});
Luke Duddridge
+1  A: 

It would be easier to move the click handler up to the <li> in this case (since that's where all the work is happening), like this:

$("ul li").click(function () {
  $(this).addClass('active').siblings().removeClass('active');
});​

Give it a try here.

Nick Craver
isn't it better to leave the click handler on the <a> tag in order to degrade gracefully for browsers without JS enabled?
espais
@espais - If your links are `#` they'll have no default effect...since this is just styling, what's there to degrade? If it's on the `<a>` or the `<li>` and JS is disabled, nothing is going to happen either way :)
Nick Craver
@Nick: the #'s will be replaced with real links as i go on...i probably should have specified that
espais
@espais - Are the links leaving the page? In that case again what happens on this page doesn't matter. If they're *not*, then `click` event on the `<li>` when it bubbles is a perfectly legitimate place to put it, simpler and less DOM traversal as well :)
Nick Craver
@Nick: they will stay on the same page, but i plan on having them go to a different location (ie page.php?loc=link1 / page.php?loc=link2)
espais