When using JavaScript to create an element and importing the related CSS, I encountered an issue where I couldn't retrieve the width of the element. Here is a snippet of my code:
CSS:
#mainDiv{
position:absolute;
width:500px;
height:500px;
}
JavaScript:
var mainDiv=document.createElement("div");
mainDiv.setAttribute("id","mainDiv");
document.body.appendChild(mainDiv);
// Now I want to get the width of the 'mainDiv'
var wd=mainDiv.style.width;
console.info(wd);
Despite setting the width in the CSS to 500px, the value of 'wd' always returns as an empty string.
This led me to wonder why this was happening.
After inspecting the element using Firebug, I confirmed that the width of 'mainDiv' is indeed set to 500px.
However, I am still unable to retrieve this value in my JavaScript code.
I do not wish to manually set the width and height of 'mainDiv' within the JavaScript like this:
mainDiv.style.width='500px';
My intention is to define the size properties within the CSS file.
Any thoughts on how to tackle this issue?