How to dynamically get the content height of a div using AngularJS ?

The content height of a div can dynamically get using clientHeight and scrollHeight properties depending upon the user requirement. If a user wants to know the space required by actual displayed content including the space taken by padding but not including the scrollbars, margins, or borders, then the user can use any of the following procedures that will return the height of the entire content of an element.

  • Using Element.clientHeight property
  • Using Element.scrollHeight property

Example 1: Content Height of div using clientHeight property will return the height of the entire content of an element.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
  
    <style>
        #div1 {
            height: 100px;
            width: 300px;
            border: 2px solid black;
            overflow: scroll;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksforGeeks</h1>
        <h3>Geeting Content height</h3>
  
        <div id="div1">
            Calculate content height of a div 
            on GeeksforGeek. GeeksforGeeks is
            a computer science portal which
            helps students to learn various 
            programming language and master
            data structures and algorithms. 
            There are various courses available
            to learn new skills.
        </div>
        <br>
        <button onclick="contentheight()">
            Content height of Div
        </button>
  
        <p id="p1"></p>
    </center>
  
    <script>
        function contentheight() {
            var ans = "Content-height: "
                + angular.element(document
                    .getElementById("div1").clientHeight)
                + "px<br>";
            document.getElementById("p1").innerHTML = ans;
        
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before Click on the button:

  • After Click on the button:



Example 2: Content Height of div using scrollHeight property will return the height of the entire content of an element.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
  
    <style>
        #div1 {
            height: 100px;
            width: 300px;
            border: 2px solid black;
            overflow: scroll;
        }
  
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksforGeeks</h1>
        <h3>Geeting Content height</h3>
  
        <div id="div1">
            Calculate content height of a div
            on GeeksforGeek. GeeksforGeeks is
            a computer science portal which
            helps students to learn various 
            programming language and master 
            data structures and algorithms. 
            There are various courses available 
            to learn new skills.
        </div>
        <br>
  
        <button onclick="contentheight()">
            Content height of Div
        </button>
  
        <p id="p1"></p>
    </center>
  
    <script>
        function contentheight() {
            var ans = "Content-height: "
                + angular.element(document
                .getElementById("div1").scrollHeight)
                + "px<br>";
            document.getElementById("p1").innerHTML = ans;
        
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before Click on the button:

  • After Click on the button:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.