.height()

In this chapter you will learn:

  1. .height() (getter) Syntax and Description
  2. .height(value) (setter) Syntax and Description
  3. How to get the whole document height
  4. How to get the height for a tag
  5. How to set the height for a div element
  6. How to change the window height
  7. Use click event to trigger height method
  8. Chain height function with style setting

.height() (getter) Syntax and Description

.height() (getter) gets the current computed height for the first element in the set of matched elements. .height() (getter) has no parameter and its return value is the height of the element in pixels.

.height() returns a unitless pixel value (for example, 400). .css('height') returns a value with units intact (for example, 400px).

.height(value) (setter) Syntax and Description

.height(value) (setter) sets the CSS height of each element in the set of matched elements.

value is an integer representing the number of pixels, or an integer with an optional unit of measure appended.

The return object is the jQuery object, for chaining purposes.

When calling .height(value), the value can be either a string (number and unit) or a number. If only a number is provided for the value, jQuery assumes a pixel unit.

If a string is provided, any valid CSS measurement may be used for the height (such as 100px, 50%, or auto).

Get the whole document height

The following code gets document height.

<html><!--from   j  a  v  a 2 s  . c  o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             alert($(document).height());
        });
    </script>
    <style>
    div{
        width:200px;
        height:100px;
        overflow:auto;
    }
    h1 { width:1000px;height:1000px; }
  </style>

  </head>
  <body>
    <body>
       <div><h1>java2s.com</h1></div>
    </body>
</html>

Click to view the demo

Get the height for a tag

The following code gets the tag height.

<html><!--   ja v  a2 s . c o  m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             alert($("div").height());
        });
    </script>
    <style>
    div{
        width:200px;
        height:100px;
        overflow:auto;
    }
    h1 { width:1000px;height:1000px; }
  </style>

  </head>
  <body>
    <body>
       <div><h1>java2 s. com</h1></div>
    </body>
</html>

Click to view the demo

Set the height for a div element

The following code sets the height for a div element.

<html><!--from j av a  2s  .com-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("div").height(300);
        });
    </script>
    <style>
    div{
        width:200px;
        height:100px;
        overflow:auto;
    }
    h1 { width:1000px;height:1000px; }
  </style>

  </head>
  <body>
    <body>
       <div><h1>java2s.com</h1></div>
    </body>
</html>

Click to view the demo

Change the window height

Window height can be changed by using height method as well.

<html><!--   j a  va 2s .  c  o m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
           alert($(window).height());
        });
    </script>
  </head>
  <body>
    <body>
         <p>Hello</p><p>java2s.com</p>
    </body>
</html>

Click to view the demo

Click to get the height

The following code passes the value from .height() to a function.

<html><!--from  j a  va 2  s  . c  om-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             function showHeight(ele, h) {
               alert( h + "px.");
             }
             $("p").click(function () {
               showHeight("paragraph", $("p").height());
             });
        });
    </script>
  </head>
  <body>
    <body>
         <p>Hello</p><p>Paragraph</p>
    </body>
</html>

Click to view the demo

Chain height function with style setting

The following code chains the .height() function with .css() method.

<html><!-- j ava2s  .  c o m-->
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                    $("div").one('click', function () {
                      $(this).height(30).css({cursor:"auto", backgroundColor:"green"});
                    });
        });
    </script>
    <style>
      div { 
         width:50px; 
         height:70px; 
         float:left; 
         margin:5px;
         background:rgb(255,140,0); 
         cursor:pointer; 
       }
    </style>
  </head>
  <body>
    <body>
          <div></div>
          <div>java2s.com</div>

    </body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. .html() method description
  2. Syntax for .html() (getter)
  3. Syntax for .html() (setter)
  4. How to use .html() to get and set HTML content
  5. How to use .html(function) to create content dynamically
  6. How to set paragraph content and apply style