I'm trying to get the dimensions of some images. Sometimes I get the dimensions and sometimes I don't. I believe it's depends on whether the images have been loaded or not?
I'm trying to get the max width and height of the images in an element. This is the code:
var maxImageHeight = 0;
var maxImageWidth = 0;
textElement.find("img").each(function() {
console.log($(this).height());
console.log($(this).width());
if($(this).height() > maxImageHeight) {
maxImageHeight = $(this).height();
}
if($(this).width() > maxImageWidth) {
maxImageWidth = $(this).width();
}
});
Sometime the console.log output yields the correct dimensions and sometimes it's just 0.
I guess I somehow have to wait until all the images have been loaded? Thanks for any help.
Here is a jsfiddle example, which seems to work every time. https://jsfiddle.net/z0eayxfw/2/