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

Using JavaScript to Set the Width of a Div

March 5, 2022 Leave a Comment

We can use JavaScript to set the width of a div by using the Style width Property.

document.getElementById("div1").style.width = "100px";

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 width Property in the following JavaScript code.

document.getElementById("div1").style.width = "200px";

We can also set the width of a div using jQuery with the jQuery width() method.

Example of Using JavaScript 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:300px; 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" onclick="changeWidth()">Change width</div>
</div>

We can utilize the onclick event to run a function when a user enters a new width.

We can also use the value property to get the new width the user has entered. We will then use the width Property to set the new width to #div2, based on what the user has entered.

Here is the code:

function changeWidth(){
  //Get the new width the user has entered
  var newWidth = Number(document.getElementById("new-width").value) + "px";

  //Set the new width of #div
  document.getElementById('div2').style.width = newWidth;
};

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

Code Output:

Enter a new width below:

Change width

Full Code:

<style>#div2 { width:300px; 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" onclick="changeWidth()">Change width</div>
</div>

<script>

function changeWidth(){
  var newWidth = Number(document.getElementById("new-width").value) + "px";
  document.getElementById('div2').style.width = newWidth;
};

</script>

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Remove Quotes From a String
  • 2.  Swapping Images in JavaScript
  • 3.  Remove Undefined Values From an Array in JavaScript
  • 4.  Using JavaScript to Get Date Format in dd mm yyyy
  • 5.  parseInt() JavaScript – Convert String to Integer with parseInt() method
  • 6.  setInterval JavaScript – How to Repeat Code in Set Time Intervals
  • 7.  How to Use JavaScript to Round Up Numbers
  • 8.  How to Split a Number into Digits in JavaScript
  • 9.  Using JavaScript to Get the Width of an Element
  • 10.  Using JavaScript to Convert Double to Integer

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