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()
.
Note: Callers from XUL documents should wait until the
onload
event has fired before calling this method.Syntax
var element = document.elementFromPoint(x, y);
Parameters
- x
- A horizontal position within the current viewport.
- y
- A vertical position within the current viewport.
Return value
The topmost Element
object under the given points.
Example
<!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>
Demo
Specifications
Specification | Status | Comment |
---|---|---|
CSS Object Model (CSSOM) View Module The definition of 'elementFromPoint' in that specification. |
Working Draft | Initial definition. |
Browser compatibility
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) | ? | ? | ? | ? |
Document Tags and Contributors
Tags:
Contributors to this page:
CollinGrimm,
erikadoyle,
Sebastianz,
Nickolay,
fscholz,
teoli,
jpmedley,
loktar,
ziyunfei,
kscarfone,
nehasharma,
Sheppy,
ethertank,
paul.irish,
Annevk,
Matej Lednar,
Bzbarsky,
Ben Karel,
Mgjbot,
BenoitL
Last updated by:
CollinGrimm,