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

185
Views
How to capture the most exterior element when click event occurs

I need to get the most element when clicking in a div... Such as this...

 <div id="show_trash_id" class="switch switch-xs">
   <input type="checkbox"/>
   <span class="slider round">
     <span class="text-on">ON</span>
     <span class="text-off">OFF</span>
   </span>
 </div>

I think that this is related to some sort of bubbling or capturing effect., however I cannot handle that... I have already tried to do event.stopPropagation() or using useCature both unsuccessfully.

function capture (event) {
   event.stopPropagation();
   console.log(event.target);
}
document.querySelector('#show_trash_id').addEventListener('click', capture, true) 

In short what I want is to get the following bellow displayed in the console:

     <div id="show_trash_id" class="switch switch-xs">
       <input type="checkbox"/>
       <span class="slider round">
         <span class="text-on">ON</span>
         <span class="text-off">OFF</span>
       </span>
     </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have had this problem before. My solution has always been to use an anonymous function and use the this keyword instead of event.target, as it refers to the element the event listener was added to.

Note however that the anonymous function cannot be an arrow function, as this in the context of an arrow function is the window.

document.querySelector('#show_trash_id').addEventListener('click', function(e) {
  console.log(this);
});
 <div id="show_trash_id" class="switch switch-xs">
   <input type="checkbox"/>
   <span class="slider round">
     <span class="text-on">ON</span>
     <span class="text-off">OFF</span>
   </span>
 </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!