DocumentOrShadowRoot.elementFromPoint()

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The elementFromPoint() property of the DocumentOrShadowRoot interface returns the topmost element at the specified coordinates (relative to the viewport).

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.

Elements with pointer events set to none will be ignored, and the element below it will be returned.

If the method is run on another document (like an iframe's subdocument), the coordinates are relative to the document where the method is being called.

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().

Syntax

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

Parameters

x
The horizontal coordinate of a point.
y
The vertical coordinate of a point.

Returns

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
Shadow DOM
The definition of 'DocumentOrShadowRoot' in that specification.
Obsolete Initial definition.

Browser Compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support53 Yes Yes Yes40 Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support5353 Yes Yes40 Yes6.0

Document Tags and Contributors

Last updated by: rsinohara,