All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Height Of DIV JavaScript

Last Updated : Mar 11, 2024

Get Height Of DIV JavaScript

In this tutorial we will show you the solution of get height of div JavaScript, here we used jquery for collect height of div element, initially we are not sets height of div element.

So we needs to import external open source jquery library support then only we can get div element height by using height() method then we prints result on console panel.

Step By Step Guide On Get Height Of DIV JavaScript :-

Here we defined div, button element. The div element defined with some styles, and button ‘Get Height’ will displays the height of div element.

In script when user clicks on button ‘.gh’ then click event loads some code, there we collecting div element ‘cont’ height by height() method and stored on variable ‘ght’ then printed result on console by console.log() method.

<!DOCTYPE html>
<html>
    <head>
        <title>GET DYNAMIC HEIGHT OF DIV</title>
        <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    </head>
    <body>
        <style>
            #cont{
                border: 5px solid rgb(130, 127, 127);
                padding: 10px;
                width: 20%;
            }
        </style>
        <div id="cont">
            Here we are going to show you height of div when clicks on Get Height button.
        </div>
        <button class="gh">Get Height</button>
        <script>
            $(document).ready(function(){
                $(".gh").click(function(){
                    var ght=$("#cont").height();
                    console.log(ght+"px");
                });
            });
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. For this program we used jquery code so needs to import open source jquery cdn link with the help of script tag.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In style tag we defined styles of ‘sets border width to 5px with solid style border, padding sets to 10px , width sets to 30%’ for id ‘cont’ of div element.
  8. Here we defined div element with text ‘Here we are going to show you height of div when clicks on Get Height button.’ and id ‘cont’ and button element ‘Get Height’ with class ‘gh’.
  9. In script we defined ready function for loads when program executes, within that we defined click event with button element ‘gh’ for loads function when user clicks on button ‘Get Height’.
  10. There we collecting div element ‘cont’ height by height() method then value stored on variable ‘ght’. Next we prints that value ‘ght’ on console panel.
  11. For seeing console result we needs to use shortcut ‘ctrl+shift+j’ then console panel will open with height of div in px.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get div element height using JavaScript.

When we executes program on browser we can see div content with box and Get Height button now user needs to clicks on Get Height button then we can get height of div and printed on console.

Must confirm one thing before execution network connection without internet code can’t executed successfully.

I hope this tutorial on get height of div JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪