I have an image wrapper div
to contain image and caption.
Using the following the image wrapper div
is sized to be the width of the image:
$('.imgright').each(function(){
$(this).width($(this).find('img').outerWidth());
});
I also wish to use a different image path if dependent on a parent div
class. The following works:
$('.grid_12').each(function(){
$(this).find('img').each(function(){
$(this).attr("src", $(this).attr('src').replace('/StandardImage/','/MidSizeImage/'));
});
});
But putting these together does not always set the width of the image wrapper div
.
$('.grid_12').each(function(){
$(this).find('img').each(function(){
$(this).attr("src", $(this).attr('src').replace('/StandardImage/','/MidSizeImage/'));
});
});
$('.imgright').each(function(){
$(this).width($(this).find('img').outerWidth());
});
Any suggestions?
Thanks to 2 very quick answers I have restructured my code and used the .load()
function to get the changed image details.
Working code is:
$('.grid_12').each(function(){
$(this).find('img').each(function(){
$(this).attr("src", $(this).attr('src').replace('/StandardImage/','/MidSizeImage/'));
$(this).load(function(args){
var newWidth = $(this).outerWidth();
$(this).parent('.imgright').each(function(){
$(this).width(newWidth);
})
})
})
});