JavaScript Code

How to change height of a div in JavaScript?


Tutorials using JS

Problem Statement

change height of a div element using JavaScript

Solution

to set or change the height of a div element using JavaScript, we can use Element.style.height property. get the div element and set its style.height property with the required length value

divElement.style.height = '200px';

Program

when user clicks on the Click me button, we get the div element with the id myDiv and set its height to 200px using style.width property

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Tutorial</h1>
<button id="myBtn">Click me</button>
<div id="myDiv">About</div>

<style>
#myDiv {
  width: 100px;
  height: 100px;
  background: green;
}
</style>

<script>
document.getElementById("myBtn").addEventListener("click", function() {
    //get the div element
    const myDiv = document.getElementById("myDiv");
    //set the height of the div
    myDiv.style.height = '200px';
});
</script>

</body>
</html>


Copyright @2022