Ghost Echo Ghost Echo - 1 year ago 186
CSS Question

jQuery ToggleClass - remove 2 classes and add 1 class

I want to do something like


<div class="box a b"></div>


$('.box').removeClass('a b').addClass('c');

but is this possible using only

Answer Source

You can use this to toggle the classes:

$('.box').toggleClass('a b c')

It will remove a and b and add c

And a working example:

$(function() {
  $('.box3').toggleClass('a b')
  $('.box4').toggleClass('a b c')
.a {
  color: red;
.b {
  background: blue;
.c {
  color: blue;
  background: red;
<script src=""></script>
<div class="box1 a b">asd</div>
<div class="box2 a b">asd</div>
<div class="box3 a b">asd</div>
<div class="box4 a b">asd</div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download