• 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 / Resizing an Image Using jQuery

Resizing an Image Using jQuery

March 31, 2022 Leave a Comment

Resizing an image using jQuery is easily done by using the css() method. We can target the width and height properties using the css() method and use that to resize our image.

$("#image").css("width","50%");

Let’s say we have the following HTML:

<div id="div1">
  <img 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 jQuery css() method in the following jQuery code.

$("#div1 img").css("width","150px");
$("#div1 img").css("height","150px");

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

jQuery("#div1 img").css("width","150px");
jQuery("#div1 img").css("height","150px");

Note this can also be done in pure JavaScript as well.

Using jQuery to Resize an Image with a Click

To resize an image using jQuery, we can combine the css() method with a click 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 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">Resize image</div>
</div>

We can utilize both the jQuery css() method and the jQuery click() method to resize the image.

We can then use the css() method to target the width and height properties 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 text() method.

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

$("#click-me").click(function(){
  var random = Math.round(Math.random() * 100);
  var newWidth = random + "%";
  $("#div1 img").css("width", newWidth);
  $("#result").text("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 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">Resize image</div>
</div>

<script>

$("#click-me").click(function(){
  var random = Math.round(Math.random() * 100);
  var newWidth = random + "%";
  $("#div1 img").css("width", newWidth);
  $("#result").text("width: " + newWidth);
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery in resizing an image.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Select Multiple Ids
  • 2.  How to use jQuery to Make an Input Field readonly
  • 3.  How to Set All Checkbox to Checked in jQuery
  • 4.  Using jQuery to Get the Bottom Position of Element
  • 5.  Input Mask Phone Number Using jQuery
  • 6.  jQuery append – Insert Element at End of Another Element
  • 7.  Using jQuery to Add CSS Important Style
  • 8.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 9.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 10.  Using jQuery to Toggle Class of HTML Element

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