-2

The code is the following:

var b = Number(document.getElementById('someElement').style.left); //0
var c = parseInt(document.getElementById('someElement').style.left, 10); //NaN
var d = parseFloat(document.getElementById('someElement').style.left); //NaN

Why cant I get the numeric value of the position of someElement?

Toffer
  • 75
  • 9

2 Answers2

1

You need to use getComputedStyle:

//This only works if you set the value in "style"
parseInt(document.getElementById('someElement').style.left, 10);

//If the value was set in a class declaration, use this:
parseInt(
    getComputedStyle(
        document.getElementById('someElement')
    ).getPropertyValue( "left" ),
    10
);

However, if the value is "auto", then this won't work, so you need to check for that first.

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
-1

can you use offsetLeft function

var left = document.getElementById('someElement').offsetLeft;
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
fabiovaz
  • 516
  • 2
  • 9
  • 1
    `offsetLeft` is not the same thing as the `left` style. Margins are included and `float: right; left: 0px` would give a different `offsetLeft` than a non-floated element. – Don Rhummy Oct 07 '15 at 20:37
  • Altought the difference from what i wanted, that it what gets my work done - Thanks! – Toffer Oct 07 '15 at 20:48
  • @Toffer use `getComputedStyle` as I show in my answer. – Don Rhummy Oct 07 '15 at 20:57