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.
You can use
siblings
andremoveClass
methods.http://jsfiddle.net/Lb65e/
Just remove all instances of
.active
first, and then add it:You change css class by this code:
Link to jsfiddle
If you know jquery you can chain it like below.
Above code will do the trick for you. Try this demo
This will remove the active class from each li that have active and than will add to the Element which was clicked.