jQuery toggleClass

The jQuery toggleCLass() method is used to add or remove one or more classes from the selected elements. This method toggles between adding and removing one or more class name. It checks each element for the specified class names. If the class name is already set, it removes and if the class name is missing, it adds.

In this way, it creates the toggle effect. It also facilitates you to specify to only add or only remove by the use of switch parameter.

Syntax:

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

Parameters of jQuery toggleClass() method

ParameterDescription
classnameIt is a mandatory parameter. It specifies one or more class name to add or remove. If you use several classes then separate them by space.
function (index, currentclass)It is an optional parameter. It specifies one or more class names that you want to add or remove.Index: It provides the index position of the element in the set.Currentclass: It provides the current class name of the selected element.
switchIt is also an optional parameter. It is a Boolean value which specifies whether the class should be added (true) or removed (false).

Example of jQuery toggleClass() method

Let’s take an example to demonstrate the effect of jQuery toggleClass() method.

<!DOCTYPE html>  

<html>  

<head>  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  

<script>  

$(document).ready(function(){  

    $("button").click(function(){  

        $("p").toggleClass("main");  

    });  

});  

</script>  

<style>  

.main {  

    font-size: 150%;  

    color: red;  

}  

</style>  

</head>  

<body>  

<button>Toggle class "main" for p elements</button>  

<p>Hello! javatpoint.com</p>  

<p>This is popular tutorial website.</p>  

<p><b>Note:</b> Click repeatedly on the button to see the toggle effect.</p>  

</body>  

</html>

jQuery toggleClass() example 2


  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>toggleClass demo</title>  
  6.   <style>  
  7.   p {  
  8.     margin: 4px;  
  9.     font-size: 16px;  
  10.     font-weight: bolder;  
  11.     cursor: pointer;  
  12.   }  
  13.   .blue {  
  14.     color: black;  
  15.   }  
  16.   .highlight {  
  17.     background: pink;  
  18.   }  
  19.   </style>  
  20.   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  21. </head>  
  22. <body>  
  23. <p class="blue highlight">www.javatpoint.com</p>  
  24. <p class="blue">Java Tutorial</p>  
  25. <p class="blue">SQL Tutorial</p>  
  26. <p class="blue">Android Tutorial</p>  
  27. <p class="blue">HTML Tutorial</p>  
  28. <p class="blue">etc.</p>  
  29. <script>  
  30. $( "p" ).click(function() {  
  31. $( this ).toggleClass( "highlight" );  
  32. });  
  33. </script>  
  34. </body>  
  35. </html>

jQuery toggleClass() example 3


  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>toggleClass demo</title>  
  6.   <style>  
  7.   .wrap > div {  
  8.     float: left;  
  9.     width: 100px;  
  10.     margin: 1em 1em 0 0;  
  11.     padding=left: 3px;  
  12.     border: 1px solid #abc;  
  13.   }  
  14.   div.a {  
  15.     background-color: aqua;  
  16.   }  
  17.   div.b {  
  18.     background-color: burlywood;  
  19.   }  
  20.   div.c {  
  21.     background-color: cornsilk;  
  22.   }  
  23.   </style>  
  24.   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  25. </head>  
  26. <body>  
  27. <div class="buttons">  
  28.   <button>toggle</button>  
  29.   <button class="a">toggle a</button>  
  30.   <button class="a b">toggle a b</button>  
  31.   <button class="a b c">toggle a b c</button>  
  32.   <a href="#">Reset</a>  
  33. </div>  
  34. <div class="wrap">  
  35.   <div></div>  
  36.   <div class="b"></div>  
  37.   <div class="a b"></div>  
  38.   <div class="a c"></div>  
  39. </div>  
  40. <script>  
  41. var cls = [ "", "a", "a b", "a b c" ];  
  42. var divs = $( "div.wrap" ).children();  
  43. var appendClass = function() {  
  44.   divs.append(function() {  
  45.     return "<div>" + ( this.className || "none" ) + "</div>";  
  46.   });  
  47. };  
  48. appendClass();  
  49. $( "button" ).on( "click", function() {  
  50.   var tc = this.className || undefined;  
  51.   divs.toggleClass( tc );  
  52.   appendClass();  
  53. });  
  54. $( "a" ).on( "click", function( event ) {  
  55.   event.preventDefault();  
  56.   divs.empty().each(function( i ) {  
  57.     this.className = cls[ i ];  
  58.   });  
  59.   appendClass();  
  60. });  
  61. </script>  
  62. </body>  
  63. </html>  

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *