I know a lot about HTML and CSS, but I'm still pretty early in the learning curve for jQuery.
I need to set various properties of some DIVs based on the size of the image inside them.
Here's some sample HTML:
<div class="box">
<img src="../pix/onepic.gif">
</div>
and here's a function that I know works:
$('.box').click(function() {
var obj = $(this);
var imgWidth = $('img:first', obj).width();
var imgHeight = $('img:first', obj).height();
$(this).width(imgWidth * 2);
$(this).height(imgHeight * 2);
});
But I now want to run this function automatically on the whole page, not just when I click, and where there will be a number of .box es containing various sizes of images.
Among other things, replace "click" in the sample above with "load" does bugger-all.
At first the suggestion below to use "each" seemed to work; when I put this script:
$('.box').each(function() {
var obj = $(this);
var imgWidth = $('img:first', obj).width();
var imgHeight = $('img:first', obj).height();
$(this).width(imgWidth * 2);
$(this).height(imgHeight * 2);
});
before the body-close tag, I got just what I hoped for.
But then I tested some more, changing the HTML to:
<div class="box">
<p>hello world</p>
<img src="../pix/onepic.gif">
</div>
and Firefox, Chrome and IE gave me three completely different results.
Firefox has what I expected, hello world and the picture in an enlarged box.
In Chrome the div now has height and width of 0px.
In IE7 the div now has a width of 56px and a height of 60px, for no reason I can discern -- the image in question is 100x100.