Reputation: 283
I'm loading an html with references to images
How do I retrieve the real pixel width and height of each image referenced?
// loaded html:
<div id="eleWithImgs">
<img id="ph01" src=".../image01.jpg">
<img id="ph02" src=".../image02.jpg">
...
// JS:
('#parentEle').load('.../photos.html #eleWithImgs', function() {
$('#eleWithImgs').children().each(function() {
var origWidth = // get dimensions of the image
});
});
Upvotes: 0
Views: 54
Reputation: 318332
You have to wait for the image to load before you can get the dimensions
$('#eleWithImgs').children().each(function() {
this.onload = function() {
var origWidth = this.clientWidth;
}
if(this.complete) this.onload();
});
Upvotes: 1
Reputation: 67217
You can use .height()
and .width()
functions to get the width/height of those images.
Try,
$('#eleWithImgs').children().each(function() {
var origWidth = $(this).width();
var origHeight = $(this).height();
});
Upvotes: 1