Daft Daft - 2 years ago 147
jQuery Question

removeClass() if it exists

This function adds a

class to my button when I click it. The button has an arrow on it which points in the direction the panel has slid.

How could I remove the rotated class when I click the button again?

$("#btnDiv").click(function (){
$('#slidePanel').toggle( "slide",{direction: 'right'});

Something like this maybe?


Answer Source

You can use .toggleClass()


That adds it if it's missing, and removes it if it's present. There's also .is() to check for things like that:

if ($('#btnDiv').is('.rotated'))

or more simply:

if ($('#btnDiv').hasClass('rotated'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download