• 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 / Using jQuery to Set the Width of a Div

Using jQuery to Set the Width of a Div

March 5, 2022 Leave a Comment

We can use jQuery to set the width of a div by using the jQuery width() method.

$("#div1").width(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 width of.</p>
</div>

If we want to set the width of #div1 to say 200px, we will use the jQuery width() method in the following JavaScript code.

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

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

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

We can also set the width of a div using pure JavaScript with the width Property.

Example of Using jQuery to Set the Width of a Div

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

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

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

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

Here is the code:

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

  //Set the new width of #div
  $('#div2').width(newWidth);
});

The final code and output for setting the width of a div using jQuery is below:

Code Output:

Enter a new width below:

Change width

Full Code:

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

<script>

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

</script>

Hopefully this article has helped you to understand how to use jQuery to set the width of a div.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Select Multiple Ids
  • 2.  Using jQuery to Check if Element Has Class
  • 3.  Using jQuery to Hide a Div
  • 4.  jQuery first() – Get the First Element
  • 5.  Using jQuery to Change Background Color
  • 6.  Using jQuery to Change the Text Color of a Paragraph
  • 7.  Using jQuery to see if URL Contains Specific Text
  • 8.  Using jQuery to Change the Font Size of a Paragraph
  • 9.  Using jQuery to Add Class to HTML Element
  • 10.  Using jQuery to Add Required Attribute to Input Field

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