August 5th, 2013, 04:12 PM
Getting .left value from elements
Can anyone tell me why this works:
myVariable = document.getElementById('myElement').width;
gives me an 'undefined' message? The element is an image, and has already been drawn on the screen by the time the code runs.
myVariable = document.getElementById('myElement').left;
Same results in IE and Firefox...
August 5th, 2013, 04:38 PM
"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.