Skullomania
Skullomania

Reputation: 2215

How do I toggle off previous class of element when a different element is clicked using jQuery?

In my pen I am using jQuery to toggle on and off css classes when buttons are clicked.

$('.btn-icon').on('click',function(){
    $(this).toggleClass('colorMe');
)};

How would I go about removing the class from a button that has been pressed class when a new button is pressed?

Just to reiterate when I click on get past the menu button and click on a sub menu button it turns white. I want to make that button turn black when another is pressed. http://codepen.io/anon/pen/BjrPxe?editors=1111

Upvotes: 0

Views: 52

Answers (1)

$(".colorMe").removeClass("colorMe");

Run before your existing toggleClass line.

Upvotes: 1

Related Questions