jQuery | toggleClass() with Examples
The toggleClass() method is an inbuilt method in jQuery which is used to toggle or change the class which attached with selected element.
Syntax:
$(selector).toggleClass(class, function, switch)
Parameters: This method accepts three parameters as mentioned above and described below:
- class: It is the required parameter and used to specify the class name need to replace.
- function: It is optional parameter and used to specify a function that returns one or more class names. This parameter contains the index position of element and class name of element.
- switch: It is optional parameter and used to specify either true or false. By default it is true.
Return Value: This method return the selected element with the specified changes made by toggleClass() method.
Below examples illustrate the toggleClass() method in jQuery:
Example 1:
<!DOCTYPE html> < html > < head > < title >The toggleclass Method</ title > < script src = </ script > <!-- jQuery code to show the working of this method --> < script > $(document).ready(function() { $("button").click(function() { $("div").toggleClass("gfg"); }); }); </ script > < style > .gfg { font-size: 25px; background-color: yellow; min-height:120px; } div { width: 200px; min-height: 120px; background-color: lightgreen; padding: 20px; font-weight: bold; font-size: 20px; } </ style > </ head > < body > <!-- click inside this div element to see the change --> < div > < p >Hello!</ p > < p >Welcome to GeeksforGeeks.!</ p > < button >Click Here!</ button > </ div > </ body > </ html > |
Output:
Example 2:
<!DOCTYPE html> < html > < head > < title >toggle class property</ title > < script src = </ script > <!-- jQuery code to show the working of this method --> < script > $(document).ready(function() { $("button").click(function() { $("div").toggleClass(function(n) { n = 1; return "item_" + n; }); }); }); </ script > < style > .item_1 { color: green; font-weight: bold; font-size: 20px; background-color: white; border: 2px solid green; } div { text-align:center; width: 200px; min-height: 100px; background-color: lightgreen; padding: 20px; border: 2px solid black; font-weight: bold; } </ style > </ head > < body > <!-- click inside this div element --> < div > < p >Welcome to GeeksforGeeks!</ p > < button >Click Here!</ button > </ div > </ body > </ html > |
Output:
Recommended Posts:
- jQuery | eq() with Examples
- jQuery | last() with Examples
- jQuery | has() with Examples
- jQuery | one() with Examples
- jQuery | on() with Examples
- jQuery | first() with Examples
- jQuery | val() with Examples
- jQuery | after() with Examples
- jQuery | andSelf( ) with Examples
- jQuery | delay() with Examples
- jQuery | animate() with Examples
- jQuery | scroll() with Examples
- jQuery | select() with Examples
- jQuery | serialize() with Examples
- jQuery | nextUntil() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.