Babiker
Babiker

Reputation: 18798

How to get the width and height of an img element where width and height have not been set?

How to get the width and height of an img element where width and height have not been set?

Upvotes: 0

Views: 819

Answers (2)

Sangeet Menon
Sangeet Menon

Reputation: 9905

Give an Id to the image(myImage) in the HTML and then ,

<img src="myimagejpg" id="myImage"/>

the following script will give the actuall height and width of the image...

var myImage = document.getElementById("myImage ");
var myheight= myImage .height;
var mywidth = myImage .width;

Upvotes: 1

Alex
Alex

Reputation: 65942

They don't have to be explicitly set by CSS:

var img = document.getElementById("my_img");
var height = img.height;
var width = img.width;

Just like that, you have the height and width of your image element.

Upvotes: 2

Related Questions