All TalkersCode Topics

Follow TalkersCode On Social Media

JavaScript Set Height Of DIV Dynamically

Last Updated : Jul 1, 2023

JavaScript Set Height Of DIV Dynamically

In this tutorial we will show you the solution of JavaScript set height of div dynamically, here we needs to use jquery for collect height from different users dynamically then we sets user input as height for div element.

So we must import external open source jquery library support then only we can see the changes on div element otherwise we can’t see any changes on div element output.

Step By Step Guide On JavaScript Set Height Of DIV Dynamically :-

Here we defined div, input tag, button elements. The div element helps us to show changes on div height, input tag for collects dynamic height size from different users, button will loads function when user clicks on it.

In script we retrieving user input by class ‘inpt’ and stored on variable ‘h’ then using height() method we sets input to div element by using id ‘cont’.

<!DOCTYPE html>
        <title>SET DYNAMIC HEIGHT FOR DIV</title>
        <script src=""></script>
                border: 5px solid rgb(130, 127, 127);
                padding: 10px;
                width: 30%;
        <div id="cont">
            We can set height for this box dynamically
        <input type="text" class="inpt" placeholder="Enter Height for Div">
        <button>Set Height</button>
                    var h=$(".inpt").val();
  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 ‘We can set height for this box dynamically’, id ‘cont’ and input element defined with placeholder, class attributes ‘inpt’ and button element ‘Set Height’.
  9. In script we defined ready function for loads when program executes, within that we defined click event with button element for loads function when user clicks on button ‘Set Height’.
  10. There we collecting user input by val() method and class ‘inpt’ then value stored on variable ‘h’. Next we sets that value ‘h’ to id ‘cont’ as height by height() method.
  11. 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 set div elements height dynamically using javascript.

When we executes program on browser we can see div content with box, input field and Set Height button now user needs to fill input box by giving height then clicks on Set Height button then we can see div height get increased on webpage based on user input.

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

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪