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

81
Views
How can I click an element positioned over another element without triggering both click events?

JSFiddle example

Is there a way to trigger the click event of a div inside another div without triggering the div on the containing elements click event?

<style>
#div1 {
  background-color: black;
  height: 200px;
  width: 200px;
  position: relative;
}

#div2 {
  background-color: white;
  height: 50px;
  width: 50px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>

<div id="div1">
  <div id="div2">
  </div>
</div>

<script>
  const div1 = document.getElementById('div1')
  const div2 = document.getElementById('div2')

  div1.onclick = () => {
    console.log("Div 1")
  }

  div2.onclick = () => {
    console.log("Div 2")
  }
</script>

How can I click the inner div without triggering click event of outer div?

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

0

You may add e.stopPropagation() to the event.

div2.onclick = (e) => {
  e.stopPropagation();
    console.log("Div 2")
}

enter image description here

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!