3

Is there a javascript (or jQuery) function that will allow you to pass in an X and Y coordinate and have it return the DOM element at that position?

I have found functions like element.getBoundingClientRect() and jQuery's offset() and position(), but these are doing the reverse of what im looking for. They provide the coordinates of an element.

Im hoping that the solution isnt to index all of the elements and their coordinates into some kind of map/array and traverse it. As anytime you moved any elements or scrolled the page you'd need to re-index the entire map/array.

haxxxton
  • 6,422
  • 3
  • 27
  • 57
  • 1
    You say "**the** DOM element at that position" as if there will be only one, but the parent of an element at X,Y will also (usually) be at X,Y, as will the grandparent, etc. Are you looking for the lowest level child element at that position? What about absolutely or relatively positioned elements that result in overlap? – nnnnnn Aug 21 '14 at 00:40
  • 4
    try [elementFromPoint()](https://developer.mozilla.org/en-US/docs/Web/API/document.elementFromPoint) – charlietfl Aug 21 '14 at 00:41
  • @nnnnnn yes the lowest level child element. Relative and Absolute positioning would need to be accounted for.. any item covering another item due to positioning would need to select the highest z-index'ed item.. it looks like `elementFromPoint()` is exactly what i needed :) – haxxxton Aug 21 '14 at 00:46
  • Possible duplicate of [Get element at specified position - JavaScript](https://stackoverflow.com/questions/1259585/get-element-at-specified-position-javascript) – Andrey Stukalin Oct 04 '19 at 09:51
  • If you are in a mouse event (`onClick` `onMouseDown`) then you can always use the target of the event argument. – Chris Murphy Jun 17 '22 at 16:18

0 Answers0