Home >>jQuery Tutorial >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 | 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
|
switch | It is optional parameter and is used to specify a Boolean value |
<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>
This is First paragraph.
This is Second paragraph.