Ghost Echo
Ghost Echo

Reputation: 2067

jQuery ToggleClass - remove 2 classes and add 1 class

I want to do something like

HTML

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

jQuery

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

but is this possible using only $.toggleClass?

Upvotes: 1

Views: 295

Answers (1)

Dekel
Dekel

Reputation: 62546

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() {
  $('.box2').toggleClass('a')
  $('.box3').toggleClass('a b')
  $('.box4').toggleClass('a b c')
});
.a {
  color: red;
  }
.b {
  background: blue;
  }
.c {
  color: blue;
  background: red;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></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>

Upvotes: 1

Related Questions