0

I'm trying to pull CSS data out of an element but I am hitting a wall for some reason. I have been staring and trying to tweak the same line of code for an hour and I keep getting undefined or NaN.

Here is the abbreviated js:

var newVar = document.getElementById("cssItem").style.top;

The css:

#cssItem {
position:   absolute;
top:        100px;
left:       100px;
width:      600px;
height:     400px; 
}

Again, every time I log newVar it comes back undefined or NaN.

1 Answer 1

2

Node.style only works for inline styles. If you want to get the rendered style, use this:

var newVar = window.getComputedStyle( document.getElementById("cssItem") ).top;

Here's the fiddle: http://jsfiddle.net/q93B6/

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

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.