Set a HTML elements width attribute right away using javascript <p>Is it possible to set an elements width like this:</p> <pre><code>&lt;img id="backgroundImg" src="images/background.png" alt="" width="javascript:getScreenSize()['width']+px" height="1100px"/&gt; </code></pre> <p>Where getScreenSize() is a javascript function.</p> <pre><code> function getScreenSize() { var res = {"width": 630, "height": 460}; if (document.body) { if (document.body.offsetWidth) res["width"] = document.body.offsetWidth; if (document.body.offsetHeight) res["height"] = document.body.offsetHeight; } if (window.innerWidth) res["width"] = window.innerWidth; if (window.innerHeight) res["height"] = window.innerHeight; alert( res['width'] ); return res; } </code></pre> <p>Or do I have to change the img's width inside a javascript function?</p> <pre><code>function changeImgWidth( img ) { img.offsetRight = getScreenWidth()['width'] + "px"; } </code></pre>