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.


var element = document.elementFromPoint(x, y);


A horizontal position within the current viewport.
A vertical position within the current viewport.

Return value

The topmost Element object under the given points.


<!DOCTYPE html>
<html lang="en">
<title>elementFromPoint example</title>

function changeColor(newColor) {
  elem = document.elementFromPoint(2, 2); = newColor;

<p id="para1">Some text here</p>
<button onclick="changeColor('blue');">blue</button>
<button onclick="changeColor('red');">red</button>


Specification Status Comment
CSS Object Model (CSSOM) View Module
The definition of 'elementFromPoint' in that specification.
Working Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support  4.0 3 5.5 10.50 (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? (Yes) ? ? ? ?


© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.

API CSSOM View Method NeedsMarkupWork NeedsMobileBrowserCompatibility Reference