Jquery : How to set the height of a div element dynamically?

Jquery : How to set the height of a div element dynamically?

Asked on November 1, 2018 in jQuery.
Add Comment


  • 1 Answer(s)

      jQuery height() method help to set the height of a <div> box dynamically .

      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.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>jQuery Set a DIV height</title>
    <style type="text/css">
      .box
    {
        background: #f2f2f2;
        border: 1px solid #ccc;
    }
    </style>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function()
    {
      $(".set-height-btn").click(function()
    {
        var newHeight = $(".input-height").val();
        $(".box").height(newHeight);
      });
    });
    </script>
    </head>
    <body>
      <form>
        <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>
      </form>
      <br>
      <div class="box">This is simple DIV box</div>
    </body>
    </html>
    
    Answered on November 1, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.