Simon
Simon

Reputation: 23141

google chrome, can't fix img width, height

if i have an image element where i'm not set height and width, like this

<img id="img" src="image.jpg" />

and then when i try to get

img_height = $("#img").attr("height");

it returns 0.

how can i get the size of image in google chrome.

Thanks

Update:

height = $("#img").height(); doesn't work too...

Upvotes: 2

Views: 865

Answers (4)

Lekensteyn
Lekensteyn

Reputation: 66405

That returned 0 because the attribute 'height' is not set. Try this jQuery function instead:

img_height = $("#img").height();

Upvotes: 1

Andy
Andy

Reputation: 856

Does this work in other browsers? Your HTML does not indicate a specified "height" attribute.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

You can use the .height() and .width() methods, like this:

var height = $("#img").height();
var width= $("#img").width();

Also make sure you're running this in $(window).load() rather than $(document).ready() so that the image is loaded.

Upvotes: 5

Stephen
Stephen

Reputation: 18964

You can use jQuery's .height() & .width()

EDIT: Now with caching to make the internet a better place:

var img_height, img_width, the_img = $("#img");

img_height = the_img.height();
img_width = the_img.width();

Upvotes: 4

Related Questions