The easiest option is to use elementFromPoint
:
var element = document.elementFromPoint(x, y); element.focus();
In addition, you can write your own function. This is what first occurred to me - I used it some time ago, when there was some reason for elementFromPoint
not working correctly, I donβt remember what exactly. There are likely to be better ways to do this, but I just tried what I was thinking before:
var coords = [100, 100], elems = document.getElementsByTagName("*"); for(var i = 0; i < elems.length; i++) { var left = elems[i].offsetLeft, top = elems[i].offsetTop, width = elems[i].offsetWidth; height = elems[i].offsetHeight; if((left <= coords[0]) && (left + width >= coords[0]) && (top <=coords[1]) && (top + height >= coords[1])) { elems[i].focus(); } }
You can see how it works here .
source share