8

I m trying to retrieve the width of an div for my function with javascript:

#areaDraw {
margin-top:50px;
width: 50%;
min-height: 100%;
z-index: 1000;
}

and the function:

 Event.add(window, "resize", function() {
    sketch.resize(document.getElementById("areaDraw").style.width,   window.innerHeight);
}).listener();

somehow javascript always returns 0 for the width of the div(areaDraw)

so whats wrong with:

document.getElementById("areaDraw").style.width
4
  • 1
    I would recommend jQuery as it has a more robust .width() function, but if you are locked into pure JS. Use offsetWidth. Commented Jul 17, 2013 at 12:12
  • Posisble duplicate stackoverflow.com/questions/6480125/… Commented Jul 17, 2013 at 12:12
  • possible duplicate of How to find the width of a div Commented Jul 17, 2013 at 12:17
  • For future visitors, it may be worth checking if your browser's developer tools panel is interfering with the element being measured. Try opening developer tools in its own window. Commented Jun 18, 2018 at 21:00

6 Answers 6

6

Try document.getElementById("mydiv").offsetWidth instead of .style.width

Sign up to request clarification or add additional context in comments.

7 Comments

works, and how can i get the Height? If i set #areaDraw to height: 30% for example?
instead of offsetWidth you would use offsetHeight
@EmSta give this a read as a refrence, w3schools.com/jsref/dom_obj_all.asp
The thing is, i tried offsetHeight, but somehow it wont work!
@EmSta could you post your code in a fiddle? Would make helping you easier
|
4

Use this instead:

document.getElementById("areaDraw").offsetWidth

Edit: As requested, an explanation of why this works (just as an extra reference).

It's because the property style.width is the width as defined explicitly in the CSS, wheareas offsetWidth finds the actual width of the element as it's displayed in the browser.

2 Comments

You could also explain why the first option did not work, and why this one does.
or you could read this answer, that does explain why :) stackoverflow.com/a/6480217/542251
1
document.getElementById("areaDraw").offsetWidth

try this one :)

Comments

1

Explanation

Your #areaDraw div element is probably empty at the moment you try to get the width of it.

element.style.width gets the width of the content of the element, regardless the padding or margin attributes.


Solution

Try using the .offsetWidth(Source: MDN) attribute. The difference is that it includes the full width of the element with its padding and margin attributes.


More information

See the MSDN example along with its reference.

Comments

1

You could also try using window.getComputedStyle

var elem = document.getElementById("areaDraw");
var width = window.getComputedStyle(elem, null).width;

Take into account that width will be a string in this case (e,g '290.5px')

getComputedStyle() gives the final used values of all the CSS properties of an element.

Comments

0

Element must have style="display: block;" and My solution:

intId = 0;

function resize(con)
{
    var width = con.offsetWidth;

    if (width == 0)
    {
        var resfnc = function(con)
        {
            return function()
            {
                var width = con.offsetWidth;

                if (width == 0)
                {
                    return;
                }

                clearInterval(intId);

                resize(con);
            }
        }

        intId = setInterval(resfnc(con), 50);

        return;
    }

    //...... work here
}

var resf = function (con)
{
    return function ()
    {
        resize(con);
    };
};


var con = document.querySelector("#elementID");

window.addEventListener('resize', resf(con), true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.