Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  132] [ 3]  / answers: 1 / hits: 23468  / 11 Years ago, fri, november 15, 2013, 12:00:00

Is there anyway to the positioning of any element without using e.pageX and e.pageY.



Check this fiddle



The fiddle is actually a poor attempt at what im trying to ask, but i though a visual example would be better. What i want to know is, Is it possible to find the X and Y co-ordinates of any element on the DOM by referencing using



 document.getElementByID('elementID');


or maybe



document.getElementsByTagName('TagName');


EDIT: Although i have used Jquery in the FIDDLE, I would like a possible solution using only JavaScript.


More From » html

 Answers
1

You may use



document.getElementById(elementID).offsetTop;
document.getElementById(elementID).offsetLeft;


Refer to MDN. They return the offset from the parent element. If you need the offset of the element in respect to the whole body it may get more tricky, as you will have to sum the offsets of each element in the chain.

Respectively for .getElementsByTagName, as each object in the DOM has these attributes.



.getBoundingClientRect is also worth a look.



var clientRectangle = document.getElementById(elementID).getBoundingClientRect();
console.log(clientRectangle.top); //or left, right, bottom

[#74271] Wednesday, November 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cortez

Total Points: 326
Total Questions: 106
Total Answers: 110

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
;