#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    15
    Rep Power
    0

    Getting .left value from elements


    Can anyone tell me why this works:

    Code:
    myVariable = document.getElementById('myElement').width;
    alert(myVariable);
    But this:

    Code:
    myVariable = document.getElementById('myElement').left;
    alert(myVariable);
    gives me an 'undefined' message? The element is an image, and has already been drawn on the screen by the time the code runs.

    Same results in IE and Firefox...
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,070
    Rep Power
    9398
    "left" only exists in CSS (as .style.left) and only has a value if you actually gave one in the CSS.

    .offsetLeft will give you the "left" relative to the parent. If you want the absolute "left" then (IIRC) you have to traverse the hierarchy, adding all the parents' offsetLeft values together. Or just use a framework like jQuery which does all that work for you.

IMN logo majestic logo threadwatch logo seochat tools logo