Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the source of an image (img) tag doesn't resize on IE

Tags:

jquery

dom

I have an img element in my DOM. Based upon user action, I compute a URL for an image, and change the image element's src attribute (using jQuery). The new image has a different size than the old image. This works fine on Safari, but IE does not resize the display for the new image's size. I do have a .load handler on the src change (in which I make some changes to the img element's class)... if I knew how to access the image's actual size, I could set the img elements height and width properties. Are those values accessible somewhere somehow? If not, is there some other way to have the image displayed at its new size?

like image 521
Zhami Avatar asked Oct 15 '22 02:10

Zhami


2 Answers

I'm working in Chrome, not IE, but I was having the same problem and thought I would share for anyone else that stumbles upon this question. I solved this by removing the "style" attribute from the image before changing the source, i.e.:

myImg.removeAttribute('style').setAttribute('src', newSrc);

like image 119
tronbabylove Avatar answered Oct 18 '22 15:10

tronbabylove


I had the same problem, but this took care of it.

$("#img").removeAttr("height").removeAttr("width").attr("src", newsrc);
like image 20
Jace Avatar answered Oct 18 '22 14:10

Jace