Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1015779 of Document.elementsFromPoint()

  • Revision slug: Web/API/Document/elementsFromPoint
  • Revision title: Document.elementsFromPoint()
  • Revision id: 1015779
  • Created:
  • Creator: Sebastianz
  • Is current revision? No
  • Comment Added live sample
Tags: 

Revision Content

{{APIRef("DOM")}}{{SeeCompatTable}}

The elementsFromPoint() method of the {{domxref("Document")}} interface returns an array of all elements at the specified coordinates.

Syntax

var elements[] = document.elementsFromPoint(x, y);

Returns

An array of {{domxref("Element")}} objects under the given point.

Parameters

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

Example

HTML Content

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

JavaScript Content

var elements = document.elementsFromPoint(30, 20);
var output = document.getElementById("output");
for(var i = 0; i < elements.length; i++) {
  var outputElement = document.createElement("p");
  outputElement.textContent = "&lt;" + elements[i].localName + "&gt;";
  output.appendChild(outputElement);
}

{{EmbedLiveSample('Example')}}

Specifications

Specification Status Comment
{{SpecName('CSSOM View', '#dom-document-elementsfrompoint', 'elementsFromPoint')}} {{Spec2('CSSOM View')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support  {{CompatChrome(43.0)}} {{CompatGeckoDesktop("46.0")}}[1] 10.0 {{property_prefix("ms")}} {{CompatUnknown}} {{CompatNo}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatChrome(43.0)}} {{CompatGeckoMobile("46.0")}}[1] {{CompatUnknown}} {{CompatUnknown}} {{CompatNo}} {{CompatChrome(43.0)}}

[1] See {{bug(1164427)}}.

Revision Source

<div>{{APIRef("DOM")}}{{SeeCompatTable}}</div>

<p>The <code><strong>elementsFromPoint()</strong></code> method of the {{domxref("Document")}} interface returns an array of all elements at the specified coordinates.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="brush: js">
var<em> elements[]</em> = <em>document</em>.elementsFromPoint(<em>x</em>, <em>y</em>);</pre>

<h3 id="Returns">Returns</h3>

<p>An array of {{domxref("Element")}} objects under the given point.</p>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt>x</dt>
 <dd>A horizontal position within the current viewport.</dd>
 <dt>y</dt>
 <dd>A vertical position within the current viewport.</dd>
</dl>

<h2 name="Example">Example</h2>

<h3>HTML Content</h3>

<pre class="brush: html">
&lt;div&gt;
  &lt;p&gt;Some text&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Elements at point 30, 20:&lt;/p&gt;
&lt;div id="output"&gt;&lt;/div&gt;
</pre>

<h3>JavaScript Content</h3>

<pre class="brush: js;highlight[1]">
var elements = document.elementsFromPoint(30, 20);
var output = document.getElementById("output");
for(var i = 0; i &lt; elements.length; i++) {
  var outputElement = document.createElement("p");
  outputElement.textContent = "&amp;lt;" + elements[i].localName + "&amp;gt;";
  output.appendChild(outputElement);
}</pre>

<p>{{EmbedLiveSample('Example')}}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('CSSOM View', '#dom-document-elementsfrompoint', 'elementsFromPoint')}}</td>
   <td>{{Spec2('CSSOM View')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>&nbsp;{{CompatChrome(43.0)}}</td>
   <td>{{CompatGeckoDesktop("46.0")}}<sup>[1]</sup></td>
   <td>10.0 {{property_prefix("ms")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(43.0)}}</td>
   <td>{{CompatGeckoMobile("46.0")}}<sup>[1]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(43.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] See {{bug(1164427)}}.</p>
Revert to this revision