Home >>jQuery Tutorial >jQuery toggleClass() Method

jQuery toggleClass() Method

jQuery toggleClass() Method

jQuery toggleClass() method in jQuery is used to toggles between removing and adding one or multiple class names from the selected elements.

Syntax:
$(selector).toggleClass(classname,function(index,currentclass),switch)

Parameter Values

Parameter Description
classname It is a required parameter and is used to specifies one or multiple class names to remove or add
function(index,currentclass) It is optional parameter and is used to specifies which returns one or multiple class names to remove/add
  • index – Used to returns the index position of the element
  • currentclass – Used to returns current class name of selected elements
switch It is optional parameter and is used to specify a Boolean value
Here is an Example of jQuery toggleClass() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$(".txt1").toggleClass("toggle");
});
});
</script>
<style>
.toggle {
font-size: 120%;
color: green;
}
</style>
</head>
<body>
<button class="btn1">click me to Toggle</button>
<p class="txt1">This is First paragraph.</p>
<p class="txt1">This is Second paragraph.</p>
</body>
</html>

Output:

This is First paragraph.

This is Second paragraph.