<p id="para1">Some text here</p>
<button>Blue</button>
<button>Red</button>Document: elementFromPoint() method / notebook
Document: elementFromPoint() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
function changeColor(newColor) {
const elem = document.elementFromPoint(2, 2);
elem.style.color = newColor;
}
document.querySelectorAll("button").forEach((button) => {
button.addEventListener("click", (event) => {
changeColor(event.target.textContent.toLowerCase());
});
});