views:

385

answers:

4

hey

I want to make a menu, and change the class when clicking.

When i click on the "li" with no class="active", i want jquery to add a class on the empty <li> and remove it from the othes "li".

<li class="active"><a href="javascript:;" onclick="$.data.load(1);">data</a></li>
<li><a href="javascript:;" onclick="$.data.load(2);">data 2</a></li>

can somebody help me ? :)

+4  A: 
// When we click on the LI
$("li").click(function(){
  // If this isn't already active
  if (!$(this).hasClass("active")) {
    // Remove the class from anything that is active
    $("li.active").removeClass("active");
    // And make this active
    $(this).addClass("active");
  }
});
Jonathan Sampson
A: 

This should get you close.

$("li").click(function() {
  $("li").removeClass("active");
  $(this).addClass("active");
});
Andy Gaskell
+2  A: 

I think you mean this:

$('li > a').click(function() {
    $('li').removeClass();
    $(this).parent().addClass('active');
});
karim79
thx alot! it works perfect..
william
+1  A: 
$('li').click(function()
{
    $('li', $(this).parent()).removeClass('active');
    $(this).addClass('active');
}
Greg