• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / JavaScript / Set the Height of a Div Using JavaScript

Set the Height of a Div Using JavaScript

February 25, 2022 Leave a Comment

To set the height of a div in JavaScript we can do this easily by using the Style height Property.

document.getElementById("div1").style.height = "100px";

Let’s take a look at a simple example below.


Let’s say we have the following HTML:

<div id="div1">
  <p>This paragraph is in a div that we want to set the height of.</p>
</div>

If we want to set the height of #div1 to say 200px, we will use the height Property in the following JavaScript code.

document.getElementById("div1").style.height = "200px";

We can also set the height of a div using jQuery with the jQuery height() method.

Example of Using JavaScript to Set the Height of a Div

In this example, we will have a simple div with a background color. The height of the div will be 100px. We will have an input field that will allow the user to enter a new height for the div. Here is the HTML code.

<style>#div2 { width:300px; height: 100px; background: #7bbfa2; }</style>
<div id="div1">
  <div id="div2"></div>
  <p>Enter a new height below:</p>
  <input type="text" id="new-height">
  <div id="click-me" onclick="changeHeight()">Change height</div>
</div>

We can utilize the onclick event to run a function when a user enters a new height.

We can also use the value property to get the new height the user has entered. We will then use the height Property to set the new height to #div2, based on what the user has entered.

Here is the code:

function changeHeight(){
  //Get the new height the user has entered
  var newHeight = Number(document.getElementById("new-height").value) + "px";

  //Set the new height of #div
  document.getElementById('div2').style.height = newHeight;
};

The final code and output for setting the height of a div using JavaScript is below:

Code Output:

Enter a new height below:

Change height

Full Code:

<style>#div2 { width:300px; height: 100px; background: #7bbfa2; }</style>
<div id="div1">
  <div id="div2"></div>
  <p>Enter a new height below:</p>
  <input type="text" id="new-height">
  <div id="click-me" onclick="changeHeight()">Change height</div>
</div>

<script>

function changeHeight(){
  var newHeight = Number(document.getElementById("new-height").value) + "px";
  document.getElementById('div2').style.height = newHeight;
};

</script>

Hopefully this article has helped you to understand how to set the height of div using JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Round to 4 Decimal Places
  • 2.  Remove Brackets from String Using JavaScript
  • 3.  JavaScript isInteger – How to Check if a Number is an Integer
  • 4.  Using JavaScript to Get a Random Number Between Range of Numbers
  • 5.  Examples Using the JavaScript += Addition Assignment Operator
  • 6.  Using JavaScript to Remove Forward Slash From String
  • 7.  How to Exit for Loop in JavaScript
  • 8.  How to Empty a String in JavaScript
  • 9.  Find Common Elements in Two Arrays Using JavaScript
  • 10.  JavaScript atan2 – Find Arctangent of the Quotient of Two Numbers

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy