I am trying to detect the position of text inside a HTML element (assume, <div>
tag) (top, left, bottom and right coordinates) on the screen. I have tried offsetLeft, and offsetWidth. But, it doesn't help. Please help.
Thanks.
You were on the right track, not sure where you got lost but you can determine coordinates using the following:
Top: node.offsetTop
Left: node.offsetLeft
Bottom: (node.offsetTop + node.offsetHeight)
Right: (node.offsetLeft + node.offsetWidth)
<div>
element. Text coordinates, width & height may vary. Right? How to find those?
May 5, 2015 at 19:42
p
tag, not the div
. Text nodes do not have the same methods as element nodes so you cannot use the offset
methods. Text Nodes cannot be targeted with CSS though, so you can safely assume the parent element's position is representative of the text node's position.