jQ UI Add Class

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery UI addClass

The jQuery addclass() method is used to allow animating the changes to the CSS properties. It adds specified classes to the matched elements while animating all style changes.

Syntax:

Following is a basic syntax of jQuery UI addclass() method: (Added in version 1.0)

 
  1. .addClass( className [, duration ] [, easing ] [, complete ] )  

Parameters of addclass () method:

  • ClassName: This is a string which contains one or more CSS classes. (In case of more CSS classes, they are separated by space.)
  • Duration:This is used to specify the time duration in milliseconds. The 0 value takes the element directly to the new style, without progress. Its default value is 400.
  • Easing: It is a type string and indicates the way to progress in the effect. Its default value is swing.
  • Complete: It is a callback method call for each element when the when the effect is completed for this element.

Syntax for jQuery addClass method: (Added in version 1.9)

The new version 1.9 of jQueryUI also supports children option, which will also animate descendant elements.

Syntax:

 
  1. .addClass( className [, options ] )   

Parameters of addclass() method in version 1.9:

className: This is a string which contains one or more CSS classes. (In case of more CSS classes, they are separated by space.)

options: It is used to specify all animation settings. All properties are optional. Its possible values are:

  • Duration: This is a type of number or string and indicates the time duration of the effect. It is counted in millisecond. The value of 0 takes the element directly in the new style, without progress. Its default value is 400.
  • Easing:It is a string. It specifies the way to progress in the effect. Its default value is swing.
  • Complete:It is a callback method called for each element when the effect is done for this element.
  • Children: This is a Boolean type and specifies whether the animation should additionally be applied to all descendants of the matched elements. Its default value is false.
  • Queue:This is of Boolean type or string type and specifies whether to place the animation in the effects queue. Its default value is TRUE.

jQueryUI addclass() example 1

Let's take an example to demonstrate the jQueryUI addclass() method (Passing single class).

 
  1. <!DOCTYPE html>  
  2. <html>  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI addClass Example</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style>  
  10.          .elemClass {  
  11.             width: 300px;  
  12.             height: 100px;  
  13.             background-color: #76eec6;  
  14.          }  
  15.          .myClass {  
  16.             font-size: 30px; background-color: #eed5b7; color: #0000ee;  
  17.          }  
  18.       </style>  
  19.       <script type="text/javascript">  
  20.          $(document).ready(function() {  
  21.             $('.button').click(function() {  
  22.                if (this.id == "add") {  
  23.                   $('#animTarget').addClass("myClass", "fast")  
  24.                } else {  
  25.                $('#animTarget').removeClass("myClass", "fast")  
  26.                }  
  27.             })  
  28.          });  
  29.       </script>  
  30.    </head>  
  31.    <body>  
  32.       <div id=animTarget class="elemClass">  
  33.          Hello! JavaTpoint  
  34.       </div>  
  35.       <button class="button" id="add">Add Class</button>  
  36.       <button class="button" id="remove">Remove Class</button>  
  37.    </body>  
  38. </html>  

Test it Now

jQuery UI addclass() example 2

Let's take an example to demonstrate the jQueryUI addclass() (Passing multiple classes)

 
  1.  <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI addClass Example</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <!-- CSS -->  
  10.       <style>  
  11.          .red { color: red; }  
  12.          .big { font-size: 5em; }  
  13.          .spaced { padding: 1em; }  
  14.       </style>  
  15.       <script>  
  16.          $(document).ready(function() {  
  17.             $('.button-1').click(function() {  
  18.                $( "#welcome" ).addClass( "red big spaced", 3000 );  
  19.             });  
  20.          });  
  21.       </script>  
  22.    </head>  
  23.    <body>  
  24.       <p id="welcome">You are here at- JavaTpoint</p>  
  25.       <button class="button-1">Click here</button>  
  26.    </body>  
  27. </html>