user2989327
user2989327

Reputation: 75

Switch classes after use .addClass on jQuery

I have some problem right now with this: http://jsfiddle.net/jalxob/WkWyc/1/

$(".show_hide").addClass( "hola1" );

The "show" link is in green and when I click on it to show the div it turns yellow, but when I click it again to hide the div the "show" link stills in yellow.

Is there any way to turn it as green (the default class)?

Thank you so much!

Upvotes: 0

Views: 63

Answers (2)

johnsoe
johnsoe

Reputation: 674

Instead of doing addClass("hola2") on your click. Do toggleClass("hola2").

This will remove the class if it is there or add the class if not.

Upvotes: 0

dsgriffin
dsgriffin

Reputation: 68586

You'd use jQuery's toggleClass() method instead of addClass() to achieve this.

$(".show_hide").toggleClass("hola2");

jsFiddle here.

Upvotes: 4

Related Questions