Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change active li when clicking a link jquery

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 ? :)

like image 329
william Avatar asked Aug 27 '09 19:08

william


People also ask

How can make Li active in jQuery?

jQuery add active class to the menu: Adding a class to List tag . i.e li element on button click. This article explains how to add a class to Li or any HTML tag using jQuery.

How can get current Li value in jQuery?

var interest = $('ul#credit'). find('li. active'). attr('interest');


3 Answers

I think you mean this:

$('li > a').click(function() {
    $('li').removeClass();
    $(this).parent().addClass('active');
});
like image 122
karim79 Avatar answered Oct 17 '22 18:10

karim79


// 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");
  }
});
like image 23
Sampson Avatar answered Oct 17 '22 16:10

Sampson


$('li').click(function()
{
    $('li', $(this).parent()).removeClass('active');
    $(this).addClass('active');
}
like image 4
Greg Avatar answered Oct 17 '22 17:10

Greg