Reputation: 6780
I'd like to set the div width equal to the image width. Here's what I can think of:
<script type="text/javascript">
window.onload = function ()
{
document.getElementById('foo').style.width = document.getElementById('bar').width + 'px';
}
</script>
<div id="foo" style="background:red;"><img id="bar" src="image.jpg"></div>
It seems to be working, but I'm not sure if it's correct coding.
Thanks in advance for checking the code or any improvement you suggest!
Rain lover
Upvotes: 1
Views: 3548
Reputation: 8966
You shouldn't have to specify a width for the div. You can give the parent div a float value.
But if you want it to truly be the same value, I believe your method will suffice.
Upvotes: 1