Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the element position - { absolute|relative|fixed } returning null value?

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??

like image 263
Vivek Chandra Avatar asked Jan 20 '26 23:01

Vivek Chandra


1 Answers

.style represents what's set on the element itself, much like the style attribute.

You could instead use getComputedStyle: http://jsfiddle.net/qAbTz/1/.

var div = document.getElementById("div");

console.log(div.style.position);              // "" (not null by the way)
console.log(getComputedStyle(div).position);​  // "fixed"
like image 128
pimvdb Avatar answered Jan 23 '26 13:01

pimvdb