W3cubDocs

/Web APIs

Document: elementsFromPoint() method

The elementsFromPoint() method of the Document interface returns an array of all elements at the specified coordinates (relative to the viewport). The elements are ordered from the topmost to the bottommost box of the viewport.

It operates in a similar way to the elementFromPoint() method.

Syntax

js

elementsFromPoint(x, y)

Parameters

x

The horizontal coordinate of a point.

y

The vertical coordinate of a point.

Return value

An array of Element objects, ordered from the topmost to the bottommost box of the viewport.

Examples

HTML

html

<div>
  <p>Some text</p>
</div>
<p>Elements at point 30, 20:</p>
<div id="output"></div>

JavaScript

js

let output = document.getElementById("output");
if (document.elementsFromPoint) {
  let elements = document.elementsFromPoint(30, 20);
  elements.forEach((elt, i) => {
    output.textContent += elt.localName;
    if (i < elements.length - 1) {
      output.textContent += " < ";
    }
  });
} else {
  output.innerHTML =
    '<span style="color: red;">' +
    "Browser does not support <code>document.elementsFromPoint()</code>" +
    "</span>";
}

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
elementsFromPoint
43Before Chrome 66, this method returned null when the element was a child of a host node. See issue 759947.
79
12–79Returns a NodeList instead of an array. See the MSDN documentation. Returns null when the point provided has no elements beneath it (e.g., when given a point outside the document).
46
10Returns a NodeList instead of an array. See the MSDN documentation. Returns null when the point provided has no elements beneath it (e.g., when given a point outside the document).
30 11.1
43Before Chrome 66, this method returned null when the element was a child of a host node. See issue 759947.
43Before Chrome 66, this method returned null when the element was a child of a host node. See issue 759947.
46 30 11.3
4.0Before Samsung Internet 9.0, this method returned null when the element was a child of a host node. See issue 759947.

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Document/elementsFromPoint