Document.elementFromPoint

Reacting to events with JavaScript is the foundation of a dynamic experiences on the web. Whether it’s a click event or another typical action, responding to that action is important. We started with assigning events to specific elements, then moved to event delegation for efficiency, but did you know you can identify elements by position […]

The post Document.elementFromPoint appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Reacting to events with JavaScript is the foundation of a dynamic experiences on the web. Whether it’s a click event or another typical action, responding to that action is important. We started with assigning events to specific elements, then moved to event delegation for efficiency, but did you know you can identify elements by position on the page? Let’s look at document.elementFromPoint and document.elementsFromPoint.

The document.elementFromPoint method accepts x and y parameters to identify the top-most element at a point:

const element = document.elementFromPoint(100, 100);
// 

If you want to know the entire element stack, you can use document.elementsFromPoint:

const elements = document.elementsFromPoint(100, 100);
// [
, , ]

The elementFromPoint and elementsFromPoint are really helpful for experiences where developers don’t want to assign individual events. Games and entertainment sites could benefit from these functions. How would you use them?

The post Document.elementFromPoint appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2022-12-30T22:37:00+00:00) Document.elementFromPoint. Retrieved from https://www.scien.cx/2022/12/30/document-elementfrompoint/

MLA
" » Document.elementFromPoint." David Walsh | Sciencx - Friday December 30, 2022, https://www.scien.cx/2022/12/30/document-elementfrompoint/
HARVARD
David Walsh | Sciencx Friday December 30, 2022 » Document.elementFromPoint., viewed ,<https://www.scien.cx/2022/12/30/document-elementfrompoint/>
VANCOUVER
David Walsh | Sciencx - » Document.elementFromPoint. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/30/document-elementfrompoint/
CHICAGO
" » Document.elementFromPoint." David Walsh | Sciencx - Accessed . https://www.scien.cx/2022/12/30/document-elementfrompoint/
IEEE
" » Document.elementFromPoint." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2022/12/30/document-elementfrompoint/. [Accessed: ]
rf:citation
» Document.elementFromPoint | David Walsh | Sciencx | https://www.scien.cx/2022/12/30/document-elementfrompoint/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.