The elementFromPoint()
method of the Document
interface returns the topmost element at the specified coordinates.
If the element at the specified point belongs to another document (for example, an iframe's subdocument), the subdocument's parent element is returned (the iframe itself). If the element at the given point is anonymous or XBL generated content, such as a textbox's scroll bars, then the first non-anonymous ancestor element (for example, the textbox) is returned.
If the specified point is outside the visible bounds of the document or either coordinate is negative, the result is null
.
If you need to find the specific position inside the element, use Document.caretPositionFromPoint()
.
onload
event has fired before calling this method.var element = document.elementFromPoint(x, y);
The topmost Element
object under the given points.
<!DOCTYPE html> <html lang="en"> <head> <title>elementFromPoint example</title> <script> function changeColor(newColor) { elem = document.elementFromPoint(2, 2); elem.style.color = newColor; } </script> </head> <body> <p id="para1">Some text here</p> <button onclick="changeColor('blue');">blue</button> <button onclick="changeColor('red');">red</button> </body> </html>
Specification | Status | Comment |
---|---|---|
CSS Object Model (CSSOM) View Module The definition of 'elementFromPoint' in that specification. | Working Draft | Initial definition. |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 4.0 | (Yes) | 3 | 5.5 | 10.50 | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | (Yes) | ? | ? | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/elementFromPoint