• 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 Resize an Image

Using JavaScript to Resize an Image

March 31, 2022 Leave a Comment

We can use JavaScript to resize an image easily by targeting the Style width and height properties and using the getElementById() method.

document.getElementById("image1").style.width = "150px";

Let’s say we have the following HTML:

<div id="div1">
  <img id="image1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
</div>

If we want to resize the image to be a set width and height, we can use the getElementById() method to target the image and then change its width and height properties in the following JavaScript code.

document.getElementById("image1").style.width = "150px";
document.getElementById("image1").style.height = "150px";

Note this can also be done in jQuery using the jQuery css() method.

Using JavaScript to Resize an Image with a Click

To resize an image using JavaScript, we can combine the getElementById() method with an onclick event.

In this example, we will have an image that we will want to resize. The image will be in a div that will have a width of 300px. The image will start at a width of 100% to fill the entire div.

We will have a button that will allow the user to resize the image to a new width each time they press it.

Here is our HTML code:

<div style="width:300px; height:100%;" id="div1">
  <img id="image1" style="width:100%;" src="http://theprogrammingexpert.com/wp-content/uploads/2021/12/tpe-main2.jpg">
  <b><div id="result"></div></b>
  <div id="click-me" onclick="resizeImage()">Resize image</div>
</div>

We will use an onclick event to run the function “resizeImage” when the user clicks a button.

We can then use the getElementById() method to target the width property of the image and use it to resize our image.

In this example, when the user clicks to resize the image, we will generate a random number from 0-100, and then set the width to that percentage.

We will let the user see the new width of the image by populating the #result div with the new width percentage using the textContent property.

Below is the JavaScript code which will allow the user to be able to resize our image by a random percentage each time:

function resizeImage(){
  var random = Math.round(Math.random() * 100);
  var newWidth = random + "%";
  document.getElementById("image1").style.width = newWidth;
  document.getElementById("result").textContent = ("width: " + newWidth);
};

The final code and output for this example on resizing an image in jQuery is below:

Code Output:


Resize image

Full Code:

<div style="width:300px; height:100%;" id="div1">
  <img id="image1" style="width:100%;" src="http://theprogrammingexpert.com/wp-content/uploads/2021/12/tpe-main2.jpg">
  <b><div id="result"></div></b>
  <div id="click-me" onclick="resizeImage()">Resize image</div>
</div>

<script>

function resizeImage(){
  var random = Math.round(Math.random() * 100);
  var newWidth = random + "%";
  document.getElementById("image1").style.width = newWidth;
  document.getElementById("result").textContent = ("width: " + newWidth);
};

</script>

Hopefully this article has been useful for you to understand how to use JavaScript to resize image.

Other Articles You'll Also Like:

  • 1.  JavaScript Map Size – Find Length of JavaScript Map
  • 2.  Math abs JavaScript – How to get the absolute value of a number using JavaScript
  • 3.  Using JavaScript to Capitalize the First Letter of a String
  • 4.  How to Remove Decimals of a Number in JavaScript
  • 5.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 6.  Using JavaScript to Check a Radio Button
  • 7.  Using JavaScript to Change the href of a Link
  • 8.  Using JavaScript to Remove Apostrophe From a String
  • 9.  Creating a JavaScript Function to Subtract Two Numbers
  • 10.  How to Get the First Character of a String in JavaScript

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