jQuery height() method sets or returns the height of the selected element.
Syntax:
When used to return the height:
$(selector).height()
When used to set the height:
$(selector).height(value)
When used to set the height using a function:
$(selector).height(function(index,currentheight))
Value:
- Value is a compulsory parameter of the jQuery height() method, as it specifies the height to set in px, em, pt, etc.
Function:
- It is an optional parameter.
- The function parameter is used to return the new height of the selected element.
Index:
- The index is an argument passed within the function.
- It is used to give an index position to an element in the set.
Currentheight:
- This parameter returns the current height of the selected element.
Example1:
<!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(){ alert("Height: " + $("div").height()); }); }); </script> </head> <body> <div style="height:100px;width:500px;padding:10px;margin:3px;border:1px solid blue;background-color:lightpink;"></div><br> <button>Height</button> </body> </html>
Example2:
<!DOCTYPE html> <html> <head> <style> div { width: 100px; height: 120px; float: left; margin: 5px; background:orange; cursor: pointer; } .mod { background: turquoise; cursor: default; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div>Tall</div> <div>Small</div> <div>Smaller</div> <div>Smallest</div> <script> var modheight = 100; $( "div" ).one( "click", function() { $( this ).height( modheight ).addClass( "mod" ); modheight -= 10; }); </script> </body> </html>