VOOZH about

URL: https://www.geeksforgeeks.org/jquery/jquery-toggleclass-method/

⇱ jQuery toggleClass() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

jQuery toggleClass() Method

Last Updated : 10 Jul, 2023

The toggleClass() method is an inbuilt method in jQuery that is used to toggle or change the class attached to the 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 is used to specify the class name that needs to replace.
  • function: It is an optional parameter and is used to specify a function that returns one or more class names. This parameter contains the index position of the element and the class name of the element.
  • switch: It is an optional parameter and is used to specify either true or false. By default it is true.

Return Value: This method returns the selected element with the specified changes made by the toggleClass() method.

The below examples illustrate the toggleClass() method in jQuery:

Example 1:

Output:

👁 jquery-39


Example 2:

Output:

👁 jquery-40


Comment
Article Tags:

Explore