jQuery .toggleClass(): Toggle a Class or Set Its State

.toggleClass("active") removes active where it is present and adds it where it is absent. .toggleClass("active", enabled) adds it when enabled is true and removes it when false. Use the first for a click that flips the state and the second to reflect a known state.

Syntax for toggling or setting a class

Pass the class name without a dot. The state argument must be an actual boolean, true or false. The string "false" and the number 0 do not act as boolean state arguments. The lines below are separate calls. jQuery API — .toggleClass()

$("#by-button").toggleClass("active");
$("#by-button").toggleClass("active", true);
$("#by-button").toggleClass("active", false);

Run a button that flips a highlight

Save the complete code in UTF-8 as jquery-toggleclass.html and open it in a browser. It uses the official CDN for jQuery 4.0.0, so it needs internet access. The button controls the first paragraph and the checkbox controls the second.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery .toggleClass() example</title>
  <style>
    body { margin: 24px; font: 1rem/1.6 system-ui, sans-serif; overflow-wrap: anywhere; }
    button, input { font: inherit; }
    .base { padding: .65rem; border: 2px solid #a1a7ad; border-radius: .4rem; }
    .active { background: #fff0b3; border-color: #08716d; font-weight: 700; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
  <main>
    <h1>jQuery .toggleClass() example</h1>
    <button id="toggle" type="button" aria-pressed="false">Toggle button highlight</button>
    <p id="by-button" class="base">Paragraph controlled by the button</p>
    <p id="button-state" role="status"></p>
    <label><input id="option" type="checkbox"> Highlight from checkbox</label>
    <p id="by-option" class="base">Paragraph controlled by the checkbox</p>
    <p id="option-state" role="status"></p>
  </main>
  <script>
    $(function () {
      const $button = $("#toggle");
      const $option = $("#option");
      const $byButton = $("#by-button");
      const $byOption = $("#by-option");
      const on = "Highlight on";
      const off = "Highlight off";

      function showButtonState() {
        const enabled = $byButton.hasClass("active");
        $button.attr("aria-pressed", String(enabled));
        $("#button-state").text(enabled ? on : off);
      }

      function syncOption() {
        const enabled = $option.prop("checked");
        $byOption.toggleClass("active", enabled);
        $("#option-state").text(enabled ? on : off);
      }

      $button.on("click", function () {
        $byButton.toggleClass("active");
        showButtonState();
      });

      $option.on("change", syncOption);
      showButtonState();
      syncOption();
    });
  </script>
</body>
</html>

Press the button once to highlight the first paragraph and change the status to Highlight on. Press again to remove active. Press Tab to focus the button, then use Enter or Space to try the same action without a mouse.

Sync the highlight with a checkbox

Check the box to add active to the second paragraph and uncheck it to remove the class. The code passes the boolean from .prop("checked") directly. Running the sync again with the same checkbox state leaves the highlight unchanged. Calling it on startup also brings the initial display into line with the checkbox.

The toggle button keeps its label and updates aria-pressed to match the state. The native checkbox already exposes its checked state. Setting a CSS class alone does not update accessibility attributes. WAI-ARIA APG — Button Pattern

Toggle several class names

Separate names with spaces, or use an array with jQuery 3.3 and later. Without a state argument, each name toggles independently. Run the snippet in the example console. The first call removes active and adds marked. The next call makes both names present.

const $item = $('<p class="base active"></p>');
$item.toggleClass("active marked");
console.log($item.attr("class")); // base marked
$item.toggleClass(["active", "marked"], true);
console.log($item.attr("class")); // base marked active

jQuery 4.0 changes and troubleshooting

jQuery 4.0 removed .toggleClass() and .toggleClass(state) when no class name is provided. Those old forms operated on the whole class list. You can still use .toggleClass("active", state) in 4.0. jQuery 4.0 Upgrade Guide

If the result looks wrong, check in Elements whether the class actually changes and whether the state is a boolean. If the class changes but the appearance does not, inspect the CSS rule and its priority. To always add or remove a class, use .addClass() or .removeClass().