<pclass="desc"><strong>Description: </strong>Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.</p>
<pclass="arguement"><strong>function(index, class)</strong>A function that returns class names to be toggled in the class attribute of each element in the matched set. Receives the index position of the element in the set and the old class value as arguments.</p>
<pclass="arguement"><strong>switch</strong>A boolean value to determine whether the class should be added or removed.</p>
</li>
</ul>
<divclass="longdesc">
<p>This method takes one or more class names as its parameter. In the first version, if an element in the matched set of elements already has the class, then it is removed; if an element does not have the class, then it is added. For example, we can apply <code>.toggleClass()</code> to a simple <code><div></code>: </p>
<p>The second time we apply <code>$('div.tumble').toggleClass('bounce')</code>, the <code><div></code> class is returned to the single <code>tumble</code> value:</p>
<p>Applying <code>.toggleClass('bounce spin')</code> to the same <code><div></code> alternates between <code><div class="tumble bounce spin"></code> and <code><div class="tumble"></code>.</p>
<p>The second version of <code>.toggleClass()</code> uses the second parameter for determining whether the class should be added or removed. If this parameter's value is <code>true</code>, then the class is added; if <code>false</code>, the class is removed. In essence, the statement:</p>
<p>As of jQuery 1.4, the <code>.toggleClass()</code> method allows us to indicate the class name to be toggled by passing in a function.</p>
<pre>$('div.foo').toggleClass(function() {
if ($(this).parent().is('.bar')) {
return 'happy';
} else {
return 'sad';
}
});</pre>
<p>This example will toggle the <code>happy</code> class for <code><div class="foo"></code> elements if their parent element has a class of <code>bar</code>; otherwise, it will toggle the <code>sad</code> class.</p>
</div>
<h3>Examples:</h3>
<divid="entry-examples"class="entry-examples">
<divid="example-0">
<h4>Example: <spanclass="desc">Toggle the class 'highlight' when a paragraph is clicked.</span>
<h4>Example: <spanclass="desc">Add the "highlight" class to the clicked paragraph on every third click of that paragraph, remove it every first and second click.</span>