jQuery: Remove class if other element is clicked
You can use siblings
and removeClass
methods.
$('li').click(function() {
$(this).addClass('active').siblings().removeClass('active');
});
http://jsfiddle.net/Lb65e/
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');
});