How to set the height of a div element dynamically using jQuery

Topic: JavaScript / jQueryPrev|Next

Answer: Use the JavaScript height() method

You can set the height of a <div> box dynamically using the jQuery height() method.

The height() method simply returns the element's height as unit-less pixel value. However calling the height(value) method sets the height of the element, where the value can be either a string (e.g. 100%, 50px, 25em, auto etc.) or a number. If only a number is provided for the value, jQuery assumes it as a pixel unit. Let's try out an example and see how it works:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Set a DIV height</title>
        background: #f2f2f2;
        border: 1px solid #ccc;
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
        var newHeight = $(".input-height").val();
        <input type="text" class="input-height">
        <button type="button" class="set-height-btn">Set Height</button>
        <p>Enter the value in input box either as number (e.g. 100, 200) or combination of number and unit (e.g. 100%, 200px, 50em, auto) and click the "Set Height" button.</p>
    <div class="box">This is simple DIV box</div>

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties