Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
detect event on 2 overlapping sibling elements?

I have 2 overlapping siblings like this:

document.querySelector("#item1").addEventListener('mouseup',()=>{
    console.log("Mouseup item 1!");
});

document.querySelector("#item2").addEventListener('mouseup',()=>{
console.log("Mouseup item 2!")
},true);
#item1{
  height:10rem;
  width:10rem;
  background-color:red;
  position:absolute;
  z-index:-2;
  
}

#item2{
  height:10rem;
  width:10rem;
  background-color:green;
  position:relative;
  top:2rem;
}
<div id="item1"></div>
<div id="item2"></div>

whenever I mouseup on item2 I want item1 mouseup event to also be triggered if mouse is within item1

( Basically I want mouseup to be triggered on whatever divs the mouse is within regardless of which is overlapping what )

How do I achieve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using elementsFromPoint as 'pointed out' by @Teemu in the comments I came up with this:

document.querySelector("#wrapper").addEventListener('mouseup', (event) => {
  let els = document.elementsFromPoint(event.clientX, event.clientY);
  els.forEach(el => {
    if (el.id != '' && el.id != 'wrapper')
      console.log(el.id)
  })
});
#item1 {
  height: 10rem;
  width: 10rem;
  background-color: red;
  position: absolute;
}

#item2 {
  height: 10rem;
  width: 10rem;
  background-color: green;
  position: relative;
  top: 2rem;
}

#wrapper {
  background-color: blue;
  height: 20rem;
  width: 20rem;
}
<div id="wrapper">
  <div id="item1"></div>
  <div id="item2"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!