jQuery height() Method

Last Updated:

jQuery height() Method

The jQuery height() method gets or sets the height of the selected elements.

Get Height

In the following example, we will get the height of the div container.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> div{ border: 2px solid black; height: 100px; width: 100px; } </style> </head> <body> <h1>jQuery height() Method</h1> <div></div> <p>Click on the button to find the height of div.</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ alert($("div").height()); }); }); </script> </body> </html>

Syntax

// To get height $(selector).height(); // To set height $(selector).height(integer); // To callback $(selector).height(function(index, height));

Parameter Values

Value Type Explanation
integer Optional Specifies the height in px.
function(index, height) Optional Specifies a function that returns the new height of selected elements
Parameters Explanation
  • index - Returns the index position of the element in the set
  • height - Returns the current height of the selected element

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
height()YesYesYesYesYesYesYesYesYesYesYesYesYes

Set Height

In the following example, we will set the height of the div container to 200px.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> div{ border: 2px solid black; height: 100px; width: 100px; } </style> </head> <body> <h1>jQuery height() Method</h1> <div></div> <p>Click on the button to set div's height to 200px.</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ $("div").height(200); }); }); </script> </body> </html>

Set Height with Callback

In the following example, we will set the height of the two div containers using a callback.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> div{ border: 1px solid black; width: 100px; height: 50px; margin: 10px; } </style> </head> <body> <h1>jQuery height() Method</h1> <div>Child 1</div> <div>Child 2</div> <p>Click on the button to change the first div's height to 100px and second div's height to 150px.</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ $("div").height(function(index, myheight){ $("div").eq(index).height(myheight * (index + 2)); }); }); }); </script> </body> </html>

Share this Page

Meet the Author