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

The Programming Expert

Solving All of Your Programming Headaches

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

Set the Height of a Div Using jQuery

February 25, 2022 Leave a Comment

To set the height of a div in jQuery we can do this easily by using the jQuery height() method.

$("#div1").height(100);

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 jQuery height() method in the following JavaScript code.

$("#div1").height(200);

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").height(200);

Example of Using jQuery 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">Change height</div>
</div>

We can utilize the jQuery click() method to run a function when a user enters a new height.

We can use the val() method to get the new height the user has entered. We will then use the height() method to set the new height to #div2, based on what the user has entered.

Here is the code:

$("#click-me").click(function(){
  //Get the new height the user has entered
  var newHeight = Number($('#new-height').val());

  //Set the new height of #div
  $('#div2').height(newHeight);
});

The final code and output for setting the height of a div using jQuery 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">Change height</div>
</div>

<script>

$("#click-me").click(function(){
  var newHeight = Number($('#new-height').val());
  $('#div2').height(newHeight);
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Parent Div of an Element
  • 2.  Using jQuery to Add Required Attribute to Input Field
  • 3.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 4.  jQuery has – Check if an Element Has Other Elements
  • 5.  Using jQuery to Add Class to HTML Element
  • 6.  Using jQuery to Set Select to the First Option
  • 7.  Using jQuery to Convert a String to Lowercase
  • 8.  Using jQuery to Hide Element by Class
  • 9.  Using jQuery to Toggle Class of HTML Element
  • 10.  How to Clear an Input Field Using jQuery

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