Jquery addclass/removeclass on click

Assuming only one active element at a time and without further knowledge of your DOM hierarchy.

$('.active').removeClass('active');
$(this).addClass('active');

More efficient options are available if the DOM hierarchy is known.

For example, if they’re all siblings, you can use this:

$(this).addClass('active').siblings('.active').removeClass('active');

Leave a Comment