.toggleClass()
Returns: jQuery
.toggleClass(className)
className One or more class names (separated by spaces) to be toggled for each element in the matched set.
.toggleClass(className, switch)
className One or more class names (separated by spaces) to be toggled for each element in the matched set.
switch A Boolean (not just truthy/falsy) value to determine whether the class should be added or removed.
.toggleClass(function(index, class), [switch])
function(index, class) 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.
switch A boolean value to determine whether the class should be added or removed.
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.
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 .toggleClass() to a simple
:
Some text.
The first time we apply $('div.tumble').toggleClass('bounce'), we get the following:
Some text.
The second time we apply $('div.tumble').toggleClass('bounce'), the
class is returned to the single tumble value:
Some text.
Applying .toggleClass('bounce spin') to the same
alternates between
and
.
The second version of .toggleClass() uses the second parameter for determining whether the class should be added or removed. If this parameter's value is true, then the class is added; if false, the class is removed. In essence, the statement:
$('#foo').toggleClass(className, addOrRemove);
is equivalent to:
if (addOrRemove) {
$('#foo').addClass(className);
}
else {
$('#foo').removeClass(className);
}
As of jQuery 1.4, the .toggleClass() method allows us to indicate the class name to be toggled by passing in a function.
$('div.foo').toggleClass(function() {
if ($(this).parent().is('.bar')) {
return 'happy';
} else {
return 'sad';
}
});
This example will toggle the happy class for
elements if their parent element has a class of bar; otherwise, it will toggle the sad class.
Example Demo < 1 >
Toggle the class 'highlight' when a paragraph is clicked.
Click to toggle
highlight
on these
paragraphs
teknosys
Popular posts from this blog
"What is eCPM? What affects my eCPM? What can I do to earn a higher eCPM?" Effective cost per thousand impressions (eCPM) is the amount of revenue you can expect to earn from AdSense for every 1000 impressions shown on your site. Since eCPM helps you measure how well your ads are performing, we often hear questions from publishers about the factors that impact this metric and how it relates to their earnings. If you're using the new interface, you'll see that your reports show RPM (revenue per thousand impressions); RPM is just another term for eCPM, and it's calculated the same way, so we use these two terms interchangeably. To help provide some clarity, we're kicking off a two-part video series with more insights into how eCPM is calculated in order to help you maximize earnings. With the help of AdSense optimization specialist, Matthew Carpenter Arevalo, we'll show you the factors that affect eCPM, how to track user behavior and traffic patterns, and ...
Comments