Kaz Nguyen
Kaz Nguyen

Reputation: 11

How to shrink image using Javascript

I'm trying to make a function that whenever I click on the button, the image on the screen width decrease by 100px, the width of the picture are all 800px here is what I have:

var width_of_pic = 800 ;

function shrink() {
  width_of_pic = width_of_pic - 100;
  var image_element= document.getElementById( "image_element_id" ) ;
  image_element.style.width = width_of_pic;
}

Upvotes: 0

Views: 54

Answers (1)

j08691
j08691

Reputation: 208002

Assuming that the ID is correct, you need to specify a unit:

image_element.style.width = width_of_pic+'px';

jsFiddle example

Upvotes: 4

Related Questions