Using javascript - we can set the element relative position such as
object.style.position="absolute"||"fixed"||"relative"
But,on using the same console.log(object.style.position)
- it does not return the position applied on the object - it returns NULL
. Am i missing something here or is there another way to achieve what i'm trying to achieve??
Using javascript - we can set the element relative position such as
object.style.position="absolute"||"fixed"||"relative"
But,on using the same console.log(object.style.position)
- it does not return the position applied on the object - it returns NULL
. Am i missing something here or is there another way to achieve what i'm trying to achieve??
- It's working : jsfiddle/9TpfT – Alexandre Khoury Commented Jun 6, 2012 at 20:08
- @mageek - not working(what i actually want) version - Fiddle – Vivek Chandra Commented Jun 6, 2012 at 20:14
- It'll return null only if it hasn't been explicitly set. If it has been set then it will return the value. – Uncle Iroh Commented Jun 6, 2012 at 20:15
-
Yes, because I don't know why, but you can't
get
the style of an object if this css isn't inline (but you canset
it). See my other answer here : stackoverflow./questions/10919859/… – Alexandre Khoury Commented Jun 6, 2012 at 20:16 - @uncle that's the problem - how to retrieve it(if it was specified in CSS) was my question.. :) – Vivek Chandra Commented Jun 6, 2012 at 20:18
2 Answers
Reset to default 6.style
represents what's set on the element itself, much like the style
attribute.
You could instead use getComputedStyle
: http://jsfiddle/qAbTz/1/.
var div = document.getElementById("div");
console.log(div.style.position); // "" (not null by the way)
console.log(getComputedStyle(div).position); // "fixed"
Note also (by the same logic presented by pimvdb), if you specify an initial position as part of the object's style, it is accessible by div.style.position.
<div id="div" style="position: absolute;"></div>
http://jsfiddle/qAbTz/4/