Why can't I get the naturalHeight/naturalWidth of an image in javascript/JQuery?
<p>So I'm trying to do some very basic div re-sizing based on the properties of the image contained within it. I know I can do something really basic like set the background an padding around the image and achieve the same effect, but this is sort of a personal challenge. </p>

<p>The problem I am having is that when I try to fetch the naturalHeight or naturalWidth values of the image, I get a value of 0. However, when I try to fetch these values using the console, they return the correct values. Also, my resizeDiv() function is coming up as undefined in console. </p>

<p>Here's the code:</p>

<pre><code>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;kitten_resize - WesGilleland.com&lt;/title&gt;
&lt;script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"&gt;&lt;/script&gt;

&lt;script type="text/javascript"&gt;
$(document).ready(function() {
    var kittens = new Array("100x100_kitten.jpeg","150x150_kitten.jpeg","200x150_kitten.jpeg");

    $('#kittens').attr('src', kittens[0]); //set the image

    function resizeDiv() {
        var img = document.getElementById('kittens');
        var imgH = img.naturalHeight;
        var imgW = img.naturalWidth;
        $('div').width(imgW);
        $('div').height(imgH);
        console.log('Current imgH: '+imgH);
        console.log('Current imgW: '+imgW);
    };

    resizeDiv();

});
&lt;/script&gt;

&lt;style&gt;
div {
    background-color: black;
    padding: 10px;
    margin: auto;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div&gt;
&lt;img id="kittens" src='' /&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>You can also find a working copy here: <a href="http://wwww.wesgilleland.com/projects/kitten_resize/" rel="nofollow">http://wwww.wesgilleland.com/projects/kitten_resize/</a></p>

<p>I feel like it's something very basic that I'm missing; I haven't fiddled with this stuff since my associate's degree a year ago. Thanks to anyone who can help!</p>
