Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the CSS left-property value of a div using JavaScript

Tags:

javascript

css

My CSS rule looks like this:

#my-div{
    display: none;
    position: absolute;
    left: -160px;
    bottom: -150px;
}

I'm trying to get value of the left-property like this:

  • document.getElementById('my-div').style.left
  • document.getElementById('my-div').offsetLeft

The problem is that both return null. Where is the problem?

like image 942
Kin Avatar asked Dec 08 '12 14:12

Kin


People also ask

How do you retrieve a CSS property value of an element?

The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.

How do I use left property in CSS?

If position: absolute; or position: fixed; - the left property sets the left edge of an element to a unit to the left of the left edge of its nearest positioned ancestor. If position: relative; - the left property sets the left edge of an element to a unit to the left/right of its normal position.

What is offset left JS?

Definition and Usage The offsetLeft property returns the left position (in pixels) relative to the parent. The returned value includes: the left position, and margin of the element. the left padding, scrollbar and border of the parent.

Can we change CSS property value using JavaScript?

CSS variables have access to the DOM, which means that you can change them with JavaScript.


2 Answers

The problem is that someElement.style.left only work if you have inline style. Since you apply your styling through a stylesheet, you will not be able to fetch the value the way you expect.

You have a couple of other approaches you could take to get the value through JavaScript:

window.getComputedStyle:

It is possible to get the computed style of an element using window.getComputedStyle, the problem is that it has quite limited support (IE9+). If you still want to use it you could wrap it up in a function to make it a bit easier to get each property:

function getCssProperty(elmId, property){
   var elem = document.getElementById(elmId);
   return window.getComputedStyle(elem,null).getPropertyValue(property);
}
// You could now get your value like
var left = getCssProperty("my-div", "left");

Working example

jQuery (or some other library):

Another option would be to use a JavaScript library like jQuery (or whatever you prefer), which will provide you with a cross-browser solution to get the value.

With jQuery you could use the .css() method to read the CSS-property of the element.

$(function () {
  var left = $("#my-div").css("left");
});

Working example

like image 95
Christofer Eliasson Avatar answered Sep 19 '22 09:09

Christofer Eliasson


You should call this

document.getElementById('my-div').style.left
document.getElementById('my-div').offsetLeft

when document is loaded, if you call it earlier it will return null because element doesnt exists yet. So you can use jQuery to determine when all content is loaded.

$(function() {
    //put your code here
});
like image 42
Sh1d0w Avatar answered Sep 21 '22 09:09

Sh1d0w