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

123
Views
Javascript event.stopPropogation is still triggering for child element of a button

I'm trying to add event listeners to buttons on a webpage, my button contains several child elements, when I click my button I only need to get the data associated with the button, such as data attributes.

Even after adding an event.stopPropogation() into my on click handler, I'm still seeing that when I click directly over a h2 element that there's no data attributes logged.

What am I exactly missing from my code?

const button = document.querySelector('[data-toggle="collapse"]')

button.addEventListener('click', (e) => {
  e.stopPropagation()
  const target = e.target.dataset
  console.log(target)
}, false)
<body>
  <button type="button" data-target="hello" data-toggle="collapse">
  <div class="row">
    <div class="col">
      <h2>
        My heading 2
      </h2>
    </div>
  </div>
</button>
</body>

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

0

Stop propagation works the other way round. You can see how it works here: What's the difference between event.stopPropagation and event.preventDefault?

What you need to do is to get the data from e.currentTarget instead of e.target.

const button = document.querySelector('[data-toggle="collapse"]')
button.addEventListener('click', (e) => {
    const target = e.currentTarget.dataset
    console.log(target)
}, false);

Also: Difference between e.target and e.currentTarget

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!