Set one element width equal to another
<p>I'd like to set the div width equal to the image width. Here's what I can think of:</p>

<pre><code>&lt;script type="text/javascript"&gt;
window.onload = function ()
{
document.getElementById('foo').style.width = document.getElementById('bar').width + 'px';
}
&lt;/script&gt;
&lt;div id="foo" style="background:red;"&gt;&lt;img id="bar" src="image.jpg"&gt;&lt;/div&gt;
</code></pre>

<p>It seems to be working, but I'm not sure if it's correct coding.</p>

<p>Thanks in advance for checking the code or any improvement you suggest!</p>

<p>Rain lover</p>
