jQuery: Remove class if other element is clicked
Asked Answered
M

10

17

I have an ul-list that contains li-elements. When the user clicks on one of these li elements a class should be added to that element.

This is easy to setup, however, when the other li-element is clicked I want the "active"-class to be removed from the non-active li.

I have made a jsfiddle of the problem: http://jsfiddle.net/tGW3D/

There should be one li-element that is red at any one time. If you click the second and then the first, only the first should be red.

Megaspore answered 2/10, 2012 at 8:44 Comment(0)
I
52

This will remove the active class from each li that have active and than will add to the Element which was clicked.

$('body').on('click', 'li', function() {
      $('li.active').removeClass('active');
      $(this).addClass('active');
});
Indiscreet answered 2/10, 2012 at 8:46 Comment(2)
it is not working for dynamically generated li. Need help.Carolincarolina
@ppmakeitcount I updated it to cover also similar casesIndiscreet
K
24

You can use siblings and removeClass methods.

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

http://jsfiddle.net/Lb65e/

Knurly answered 2/10, 2012 at 8:46 Comment(0)
S
5

Just remove all instances of .active first, and then add it:

$('ul li').on('click', function() {  
    $('ul li.active').removeClass('active');
    $(this).addClass('active');    
});
Saturated answered 2/10, 2012 at 8:47 Comment(0)
R
2
$('li').click(function() {
  $(this).siblings().removeClass('active');
  $(this).addClass('active');
});

Check: http://jsfiddle.net/tGW3D/2/

Roundworm answered 2/10, 2012 at 8:47 Comment(0)
B
2

Something like this?

http://jsfiddle.net/c7ZE4/

You can use the siblings function. addClass returns the same jquery object $(this) so you can chain the siblings method which returns all the other elements except $(this).

 $('li').click(function() {
  $(this).addClass('active').siblings().removeClass('active');
 });
Blow answered 2/10, 2012 at 8:48 Comment(1)
I see now that i wasnt alone in this one :D Anyways this can be a one liner.Blow
D
1
 $('li').click(function() {
      $(this).addClass('active'); // add the class to the element that's clicked.
      $(this).siblings().removeClass('active'); // remove the class from siblings. 
});

If you know jquery you can chain it like below.

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

Above code will do the trick for you. Try this demo

Diversify answered 2/10, 2012 at 8:50 Comment(0)
C
0
<script>
    $(function() {
        $('li').click(function() {
            $("li.active").removeClass("active");
                $(this).addClass('active');
            });
        });
</script>
Clemen answered 24/2, 2017 at 13:17 Comment(1)
You can improve the answer by providing explanation of what you doGeof
P
0
  $(document).on('click','ul li',function() {
        $(this).addClass('active').siblings().removeClass('active');
    });

This jquery code might help. Don't forget to add Jquery CDN.

Peeve answered 28/5, 2020 at 12:49 Comment(0)
M
0

You may use following code:

$('document').on('click', 'ul li', function() {
    $(this).addClass('active').siblings().removeClass("active") ;
});
Maria answered 11/2, 2021 at 23:12 Comment(1)
In addition to your code snippet, it would be nice if you could explain what is happening. Even though for most people it's self-explanatory, I'm sure there are people who would be glad if some explanation is included. Also pleas format your answer so it's more readable (add it into a code block and maybe add line breaks)Bigner
S
-1

You change css class by this code:

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

Link to jsfiddle

Sole answered 27/1, 2016 at 14:51 Comment(1)
Thanks, but the question has been answered for 4 years. :)Megaspore

© 2022 - 2024 — McMap. All rights reserved.