To get the position of a div/span tag with JavaScript, we use the getBoundingClientRect
method.
For instance, we write
const offsets = document.getElementById("11a").getBoundingClientRect();
const top = offsets.top;
const left = offsets.left;
to select the element with getElementByid
.
Then we call getBoundingClientRect
method to return an object with the top
and left
coordinates to get the x and y coordinates of the top left corner of the element.