0

I have two div with same x position. And i am trying to do that getting id of both divs from document.elementFromPoint(). Here is the problem that only id recieved of a div.

var elem = document.elementFromPoint(50, 50);
var elem2 = document.elementFromPoint(50, 250);
console.log(elem.id, elem2.id);
#box1,
#box2 {
  width: 50px;
  height: 50px;
  background: greenyellow;
}

#box1 {
  position: absolute;
  top: 50px;
  left: 50px;
}

#box2 {
  position: absolute;
  top: 50px;
  left: 150px;
}
<div id="box1"></div>
<div id="box2"></div>
  • 1
    Possible duplicate of [Get element from point when you have overlapping elements?](https://stackoverflow.com/questions/22428484/get-element-from-point-when-you-have-overlapping-elements) – Akber Iqbal Sep 14 '19 at 08:29
  • _"I have two div with same x position"_ - That's not the case (`left: 50px`, `left: 150px`). – Andreas Sep 14 '19 at 08:29

1 Answers1

0

You're pretty close, but if you check out the documentation, your parameters are switched round.

document.elementFromPoint(x, y)

Where x is left, y is top.

var elem = document.elementFromPoint(50, 50);
var elem2 = document.elementFromPoint(150, 50);
console.log(elem.id, elem2.id);
#box1,
#box2 {
  width: 50px;
  height: 50px;
  background: greenyellow;
}

#box1 {
  position: absolute;
  top: 50px;
  left: 50px;
}

#box2 {
  position: absolute;
  top: 50px;
  left: 150px;
}
<div id="box1"></div>
<div id="box2"></div>
Toby Mellor
  • 8,093
  • 8
  • 34
  • 58