All posts by Thiyagu

.height() method in Jquery

Description:

The jQuery height() method is used to GET/SET the height for the selected element. In simple, height() method can be split in to two purposes

  • GET height: It will return the height for the selected/matched element
  • SET height: It will set the height for the selected/matched element

Syntax

  • $(selector).height()
  • $(selector).height(value)
  • $(selector).height(function(index,currentheight))
Value   It specifies the height in px, pt, em, etc. In default it treat as px.
function(index,currentclass) A function that returns one or more class names to remove
index : Returns the index position of the matched element
currentclass : Returns the current class name of the matched element

Difference between .css(“height”) & .height():

.css( “height” ) : It will returns with unit value (for example, 200px)
.height() : It return with units less (for example, 200).

When to use?

If we need to perform some mathematical calculation based on the element’s height then we can use .height().

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <style>
 .defaultClass {
 border: 5px solid #eed5b7;
 padding: 10px;
 width: 50px;
 height: 50px;
 background-color: red;
 display:inline-block;
 }
 
 </style>
 <script type="text/javascript">
 $(document).ready(function () {
 $('.btnClass').click(function () {
 $('.defaultClass').height(20).css({ backgroundColor: '#4cff00' })
 $('#divCurrentHeight').text(" Current Div height is: "+ $('.defaultClass').height());
 })
 });
 </script>
</head>
<body>
 <button class="btnClass" id="addMethod">SET/GET height() CSS property</button><br/><br />
 <div id="divbox1" class="defaultClass">
 </div>
 <div id="divbox2" class="defaultClass">
 </div>
 <br /><br />
 <div id="divCurrentHeight"></div>
</body>
</html>

Output: 

Clicking on button, the .height(20) method will set height as 20 for the selected element and .height() method will GET the height of the selected div .

 

.toggleClass() method in Jquery

Description:

The jQuery toggleCLass() method is used to add or remove one or more classes from the selected elements.  In simple, the toggleClass() method checks each element for the specified class names.  If the class name is already presented for the selected element then it will remove else it will add the class.

Syntax

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

className  It specify One or more class names to add/remove from the selected set. More than one class name will be separated by the empty space
function(index,currentclass) A function that returns one or more class names to remove
index : Returns the index position of the matched element
currentclass : Returns the current class name of the matched element
state It specity the boolean value for the class should be added or removed.
add: true ,  remove: false

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <style>
 .CustomClass {
 border:2px soli<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <style>
 .defaultClass {
 border: 5px solid #eed5b7;
 padding: 10px;
 width: 400px;
 height: 100px;
 background-color: #eed5b7;
 }

 .CustomClass {
 border: 5px solid #4cff00;
 font-size: 25px;
 background-color: #4cff00 !important;
 }
 </style>
 <script type="text/javascript">
 $(document).ready(function () {
 $('.btnClass').click(function () {
 $('#divCssBoard').toggleClass("CustomClass");
 })
 });
 </script>
</head>
<body>
 <button class="btnClass" id="addMethod">Toggle CSS Class</button><br/><br />
 <div id=divCssBoard class="defaultClass">
 Welcome to dotnet-helpers.com
 </div>
</body>
</html>

Output: 

As per code,  on clicking of “Toggle CSS Class” button, the toggleClass method will verify the selected div with the “CustomClass”, if its not present then it will add the class else it will remove the class as shown below.

 

.removeClass() method in Jquery

Description:

The Jquery removeClass() method remove the css class for the matched element. It will remove single/multiple classes for each element in the set of matched elements.

Note: The removeClass method will remove all css class names from the selected elements if no parameter is specified.

Syntax

$(selector).removeClass(classname,function(index,currentclass))

className  Specifies one or more class name to remove for the selected element. More than one class will be give by “space” as separator.
function(index,currentclass) A function that returns one or more class names to remove
index : Returns the index position of the matched element
currentclass : Returns the current class name of the matched element

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <style>
 .CustomClass {
 border:2px solid #808080;
 font-size: 30px;
 background-color: #4cff00 !important;
 }

 .defaultClass {
 border: 2px solid #808080;
 width: 400px;
 height: 100px;
 background-color: #eed5b7;
 }
 </style>
 <script type="text/javascript">
 $(document).ready(function () {
 $('.btnClass').click(function () {
 if (this.id == "addMethod") {
 $('#divCssBoard').addClass("CustomClass", "fast")
 } else {
 $('#divCssBoard').removeClass("CustomClass", "fast")
 }
 })
 });
 </script>
</head>
<body>
 <button class="btnClass" id="addMethod">Add Class</button>
 <button class="btnClass" id="removeMethod">Remove Class</button><br /><br />
 <div id=divCssBoard class="defaultClass">
 Welcome to dotnet-helpers.com
 </div>
</body>
</html>

Output: 

As per our code,  on clicking of “Add CSS Class” button, it will add the “CustomClass” class to the div & if we click “Remove CSS Class” button, then it will remove “CustomClass” from the div  using removeClass() method as shown below.

 

.hasClass() method in Jquery

Description:

The Jquery hasClass() method validate whethere selected elements having a specified class name or not. If selected elements has matched class name then it will return “true” else it will return “false”.

Syntax

.hasClass( className )

className  It specify the class name to search whether its present or not

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divAdd").click(function () {

 if ($("#divBox1").hasClass("sqBoxOdd")) {
 $("#divBox1").addClass("selected");
 }
 if ($("#divBox2").hasClass("sqBoxOdd")) {
 $("#divBox2").addClass("selected");
 }
 if ($("#divBox3").hasClass("sqBoxOdd")) {
 $("#divBox3").addClass("selected");
 }
 if ($("#divBox4").hasClass("sqBoxOdd")) {
 $("#divBox4").addClass("selected");
 }
 });
 });

 </script>
 <style type="text/css">
 .selected {
 background-color: #ff6a00 !important;
 }

 .sqBoxOdd {
 display: inline-block;
 margin: 20px;
 width: 80px;
 height: 80px;
 left: 10px;
 top: 10px;
 background-color: #4CAF50;
 }

 .sqBoxEven {
 display: inline-block;
 margin: 20px;
 width: 80px;
 height: 80px;
 left: 10px;
 top: 10px;
 background-color: #4CAF50;
 }
 </style>

</head>
<body>
 <button style="margin: 20px;" id="divAdd">Add CSS Class</button><br />
 <div id="divBox1" class="sqBoxOdd"></div>
 <div id="divBox2" class="sqBoxEven"></div>
 <div id="divBox3" class="sqBoxOdd"></div>
 <div id="divBox4" class="sqBoxEven"></div>
</body>
</html>

Output: 

On clicking “Add CSS Class” button then the “if” condition will execute and append the “selected” class to the matched div. The CSS class will append only if it receive true from the .hasClass() method. 

 

addClass() method in Jquery

Description:

The Jquery addClass() method add the specified class(s)  for the matched/selected elements. If we need to add more than one class then we need to use Space as the separate between the class names. It does not remove existing class attributes in the element, it only adds one or more class names to them.

Syntax

.addClass( className )
.addClass( function )

className  It specify the class name to add for the selected element. More than one class will be included by using the space as separator.
function  A function returning one or more space-separated class names to be added to the existing class name. The function accept two arguments as parameter, one is the index position of the element and  second is the existing class name. 

Note :

To set multiple class(s)

.addClass({“className1 className2”});

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divAdd").click(function () {
 $("#divBox").css("background-color", "");
 $("#divBox").addClass("selected");
 });
 });

 </script>
 <style type="text/css">
 .selected {
 background-color: #ff6a00;
 }
 </style>

</head>
<body>
 <button style="margin: 20px;" id="divAdd">Add CSS Class</button>
 <div id="divBox" style="margin: 20px;width: 80px;height: 80px;left: 10px;top: 10px; background-color:#4CAF50"></div>
</body>
</html>

Output: 

Before clicking the button Add CSS Class” button

After clicking the “Add CSS Class” button, the “selected” class has appended to the div and back-ground color has updated from the new class as shown below.

Note:

Here is the sample code for using Function as parameter.

$( “div” ).addClass(function( index, currentClass ) {// perform action based on the index/current Class name});

css() method in Jquery

Description:

Jquery .css() method Get/set the css property for the matched elements.

Syntax

GET Property: .css( propertyName )
SET Property: .css( propertyName, value )

propertyName  A css property name
value A value to set for the property

Note :

To set multiple value, the syntax will be

.css({“propertyname”:”value”,”propertyname”:”value”,…});

GET CSS Property value :

It will return the value for specified CSS property value for the matched element

Example: 

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("button").click(function () {
 alert("Background color = " + $("#divBlogName").css("background-color"));
 });
 });

 </script>

</head>
<body>
 <h2>Jquery .css() Method </h2></br>
 <div id="divBlogName" style="background-color:#00ff21">This is dotnet-helpers.com</div></br>
 <button>Display the background color</button>
</body>
</html>

Output:

SET CSS Property :

It will set CSS property for the matched element.

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("button").click(function () {
 $("#divBlogName").css("background-color","#00ff21");
 });
 });

 </script>

</head>
<body>
 <h2>Jquery .css() Method </h2></br>
 <div id="divBlogName">This is dotnet-helpers.com</div></br>
 <button>Display the background color</button>
</body>
</html>

 

Output:

Clicking the button, the background-color porperty will set to the div(divBlogName) as shown below.

serializeArray() method in Jquery

Description:

The jQuery serializedArray() Method is used to create a array of string by serializing form values. It operates on a collection of forms and its controls. We can select one or more form elements such as <input>, <textarea> or the form element itself.

Syntax

$(selector).serializeArray() 

No Arguments This method does not accept any arguments.

Example:

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#btnSubmit").click(function () {
 $("#divOutpu").text($("form").serialize());
 });
 });
 </script>
</head>
<body>
 <form>
 Enter Your First name: <input type="text" name="FirstName"><br>
 Enter Your Last name: <input type="text" name="LastName"><br>
 Enter Your Age: <input type="text" name="Age" style="margin-left:35px;"><br>
 </form>
 <br>
 <button id="btnSubmit">Serialize form values</button><br><br>
 <div id="divOutpu" style="background-color:#6fb5ef; font-size:large; width:600px;color:black;"></div> 
</body>
</html>

Output:

serialize() method in Jquery

Description:

The Jquery serialize() method creates a URL encoded text string by serializing form values. It is used in form controls like <input>, <textarea>, <select> etc.. It serializes the form values so that its serialized values can be used in the URL query string while making an AJAX request.

Syntax

$(selector).serialize() 

No Arguments This method does not accept any arguments.

Example:

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#btnSubmit").click(function () {
 $("#divOutpu").text($("form").serialize());
 });
 });

 </script>

</head>
<body>
 <form>
 Enter Your First name: <input type="text" name="FirstName"><br>
 Enter Your Last name: <input type="text" name="LastName"><br>
 Enter Your Age: <input type="text" name="Age" style="margin-left:35px;"><br>
 </form>
 <br>
 <button id="btnSubmit">Serialize form values</button><br><br>
 <div id="divOutpu" style="background-color:#6fb5ef; font-size:large; width:600px;color:black;"></div> 
</body>
</html>

Output:

clearqueue() Effect in Jquery

Description:

The Jquery clearQueue() method removes all queued items from the queue that have not yet been executed. When the .clearQueue() method is executed then all functions on the queue that have not been executed are removed from the queue. If there is no argument then the clearQueue() will remove all the remaining functions.

Syntax

$(selector).clearQueue( queueName)

queueName It contain the name of the queue. Defaults it will be fx.

Example:

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 var divBox = $("#divBox");
 $("#divStart").click(function () {
 divBox.animate({ height: 200 }, 2000);
 divBox.slideUp(2000);
 divBox.slideDown(2000);
 divBox.animate({ width: 300 }, 1500);
 divBox.animate({ height: 100 }, 1500);
 divBox.animate({ width: 100 }, 1500);
 });
 $("#divStop").click(function () {
 divBox.clearQueue();
 });
 });

 </script>

</head>
<body>
 <button id="divStart">Start Animation</button>
 <button id="divStop">Stop Animation</button>
 <br><br>

 <div id="divBox" style="background: #00ff21; height: 100px; width: 100px;"></div>
</body>
</html>

Output:

Clicking on “Start Animation”” button, the “divBox” start with animations and stop the execution after calling the clearqueue() function. From the below output, ” divBox.animate({ width: 300 }, 1500);divBox.animate({ height: 100 }, 1500);divBox.animate({ width: 100 }, 1500);”  animation steps has cleared from the queue after clicking the “stop animation” button while executing “divBox.slideDown(2000);”.

dequeue() Effect in Jquery

Description:

Execute the next function on the queue for the matched element. When jQuery.dequeue() is called, the next function on the queue is removed from the queue, and then executed.

Syntax

$(selector).dequeue( element

[, queueName ] )
element  Need to secify the DOM element to remove and execute a queued function.
queueName It contain the name of the queue. Defaults it will be fx.

Example:

<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
 <script type="text/javascript">

 $(document).ready(function () {
 $("#divstart").click(function () {
 $("#divBox")
 .animate({ left: '+=200px' }, 2000)
 .queue(function () {
 $(this).css("background-color", "#1a80b6");
 $.dequeue(this);
 })
 .animate({ left: '10px', top: '30px' }, 1000)
 .animate({ height: '200px', width: '200px' }, 1000);
 });
 });

 </script>

</head>
<body>
 <button style="margin: 20px;" id="divstart">Start Queue with Dequeue</button>
 <div id="divBox" style="margin: 50px;position: absolute;width: 80px;height: 80px;left: 10px;top: 30px;background-color: #4CAF50;"></div>
</body>
</html>

Output:

Clicking on “Start Queue with Dequeue” button, the “divBox” start with animations and skip the execution of the queue function when it reach .dequeue() method.

Note:

We need to use dequeue() method  with queue() else the queue will not be closed and you will get undesired result.