Toggle CSS Class for Each Matching Element .toggleClass()


className: {CSS class, Function(index, className, switch){...}}
e.g. $("div").toggleClass("className")

.toggleClass(className, [switch]) will add className if it does not exist or remove it if it does exist. If optional switch is used, then className will be added if switch is true and removed if false.

Example 1: $("div").toggleClass("someClass") will add someClass to each <div> if it does not have one, and remove it from each <div> that already has it.
<div class="someClass">a</div>
<div>a</div> // someClass removed
<div class="someClass">b</div> // someClass added
Example 2: $("div").toggleClass( function(index) { return $(this).text() } ); will remove class that matches <div> content and add <div> content as class if it does not exist.
<div class="a">a</div>
<div class="a">b</div>
<div>a</div> // remove a
<div class="a b">b</div> // add b
jQuery manipulation css .toggleClass()