How do i find the X,Y coordinates of a particular html element ( eg. div, table, lable, etc...) relative to the desktop screen (i.e. outside the browser window) using JavaScript?
I can find the height and width of the element by using offsetHeight and offsetWidth, but can't find anything that can give me exact X,Y coordinate of the element relative to the user’s entire desktop screen.
I think you have to follow the tree up, through the parents, and keep adding the offsets, like described here:
http://bytes.com/topic/javascript/answers/90547-how-get-absolute-position-element
function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}
I don't think it is not possible even it is quite simple.
Follow the piece of code I written:
var element = document.getElementById("ID of the element");// you can use any method to find the element ..
var position = getPosition(element);
function getPositions(obj)
{
var p = [];
var position = obj.getBoundingClientRect();
p[0] = window.screenX + position.left;
p[1] = window.screenY + position.top;
p[2] = position.width;
p[3] = position.height;
return p;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With