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

99
Views
Capture two events in two overlapped elements

I have two divs that are overlapped with two mouse-events. Is it possible to capture both events? I'm only able to capture A or B, but not A and B at the same time.

function adown() {
  console.log('A')
}

function bdown() {
  console.log('B')
}
#a {
  pointer-events: all;
  width: 200px;
  height: 200px;
  background: red;
  position: absolute;
}

#b {
  pointer-events: all;
  width: 200px;
  height: 200px;
  background: blue;
  position: absolute;
  opacity: 0.5;
  top: 50px;
  left: 50px;
}
<div id="a" onmousedown="adown()"></div>
<div id="b" onmousedown="bdown()"></div>

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

0

As mentioned in the comments, you can use document.elementsFromPoints(x,y) where x and y are from mouseposition on the document onclick event. Then, just "filter" the elements by wrapping them up in a div, in this code with id canvas. And lastly, just console.log their id.

As you can see in this snippet:

document.addEventListener("click", function(){
    let els = document.elementsFromPoint(event.clientX, event.clientY);
    els.forEach(function(el){
        if(document.querySelector("#canvas").contains(el)){
            console.log(el.id);
        }
    });
});
#A {
    pointer-events: all;
    width: 200px;
    height: 200px;
    background:red;
    position: absolute;
}
#B {
    pointer-events: all;
    width: 200px;
    height: 200px;
    background:blue;
    position: absolute;
    opacity: 0.5;
    top: 50px;
    left:50px;
}
<div id="canvas">
<div id="A"></div>
<div id="B"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

If you use document.elementsFromPoints from previous answers and use an attribute to describe which function you want to call on click you'd be able to fire two function with one click.

const functions = {
   funcA: () => console.log('A'),
   funcB: () => console.log('B'),
}

document.addEventListener("click", e => {
    const elements = document.elementsFromPoint(e.clientX, e.clientY);
    elements.forEach(el => {
        if(document.querySelector("#container").contains(el)){
        const functionAttribute = el.getAttribute('data-function');
            if(functionAttribute && functionAttribute.length && functions[functionAttribute]){
            functions[functionAttribute]();
          }
        }
    });
});
.elem {
  width: 200px;
  height: 200px;
  background-color: red;
  position: absolute;
}
<html>
<body>
<div id="container">
  <div data-function="funcA" class="elem">A</div>
  <div data-function="funcB" class="elem">B</div>
</div>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Report

0

Or you can assign both divs the same class and then iterate over them:

<div class="test"></div>
<div class="test"></div>
const testDivs = document.querySelectorAll('.test');
testDivs.forEach(testDiv => {
   testDiv.addEventListener('click', () => {
       // do something...
   });
});
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!