JavaScript document.elementFromPoint() Method
You are Here:
JavaScript document.elementFromPoint() Method
The document.elementFromPoint()
method returns the topmost Element at the specified coordinates (relative to the viewport).
Note: If the element at the specified point belongs to another document, that document's parent element is returned.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body onclick="myFunction(event)">
<h1>Click this Heading</h1>
<p>Click this paragraph</p>
<div>Click this div</div>
<script>
function myFunction(event){
var xCoords = event.clientX;
var yCoords = event.clientY;
elem = document.elementFromPoint(xCoords, yCoords);
if(elem.style.color == "red")
elem.style.color = "blue";
else
elem.style.color = "red";
}
</script>
</body>
</html>
Syntax
document.elementFromPoint(x, y)
Parameter Values
Value | Type | Explanation |
---|---|---|
x | Required | The horizontal coordinate of a point, relative to the left edge of the current viewport. |
y | Required | The vertical coordinate of a point, relative to the left edge of the current viewport. |
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.