Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery toggleClass with conditional

Does anyone know how to write the following in a neater, one line format? I'm sure it's possible but can't get very far with it.

if($('#myDiv').hasClass('hidden')){
   $('#myDiv').toggleClass('shown hidden');
}

Basically to only perform a toggle in one direction.

Many thanks,

Tom.

like image 924
Tom Millard Avatar asked Jun 09 '26 06:06

Tom Millard


1 Answers

$('#myDiv.hidden').toggleClass('shown hidden');
like image 175
Mukesh Soni Avatar answered Jun 11 '26 18:06

Mukesh Soni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!