Using JavaScript to get width of div

How can I use JavaScript to get the width of a div that isn’t explicitly set in css?

getComputedStyle()

using window.getComputedStyle(mydiv) returns ‘auto’ rather than the actual width…

http://www.quirksmode.org/dom/getstyles.html

Mike Foster, who is a member here at Sitepoint, has developed a very nice library of functions called X, that has what you’re looking for. You may wish to check it out, over at [URL=“http://www.cross-browser.com”]cross-browser.com. The library is highly configurable, allowing you to add only the functions you need, which can make for a low scripting profile.

If you use a Javascript library like Prototype, you can do this easily. I can’t post the link because I am new and do not yet have 5 posts, but just search “prototype api” and go under “Element” and “getWidth”

Hope this is what you are looking for!

I want to avoid using a full blown library as this should be a relatively simple task. I’ll check out crmalibu and Dave’s suggestions.

I was under the impression that the offsetWidth-property was x-browser?

document.getElementById(‘my-element’).offsetWidth

Yep…Thats exactly what I ended up using. @crmalibu Thanks for the link. @Dave, thanks for the x library too. I’m pretty sure I will use some of those functions in the future…

Quirksmode agrees that offsetWidth works compatibly across all major browsers.

http://www.quirksmode.org/dom/w3c_cssom.html#elementview