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

146
Views
Event Propagation With One Event Listener And Nested Children

On my website, I have some div elements with some content. Each of the elements is supposed to be clickable. I could add a separate event listener to each one, but I would rather have a single event listener attached to the container. To better explain the document structure, I have added an example below.

<div id="container">
  <div>
    <h1>Element 1</h1>
    <p>Lorem ipsum...</p>
  </div>
  <div>
    <h1>Element 2</h1>
    <p>...dolor sit amet...</p>
  </div>
</div>

Now my question is the following: I know that using event.target gets me the actual element that has been clicked, which - when using one event listener that is attached to the div with the ID container - could be the container, one of the direct children, or the text elements inside them. How can I determine which of the inner div elements - if any - the user clicked, even if the target is not the div itself?

If there is a different/simpler solution using JQuery, I would also be interested in that.

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

0

If you don't need the contents of each div (p and h1) to have interactive elements (such as form items), then you can add an extra div wrapper that is unclickable :). Now all your click targets will be the first child divs.

document.querySelector("#container").addEventListener("click", (e) => console.log(e.target))
.unclickable {
   pointer-events:none;
}
<div id="container">
          <div>
             <div class="unclickable">
               <h1>Element 1</h1>
               <p>Lorem ipsum...</p>
             </div>
          </div>
          <div>
             <div class="unclickable">
               <h1>Element 2</h1>
               <p>Lorem ipsum...</p>
             </div>
          </div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Given your comments under the question your goal is to find an effective way to determine if the user clicked on the outer div or one of the child elements.

To do that you compare the target property of the event, which is the element that raised the event, to the currentTarget property, which will always be the element the event was bound to. The logic would look something like this:

$('#container').on('click', e => {
  if (e.target == e.currentTarget) {
    console.log('clicked on parent');
  } else {
    console.log(`clicked on child element - ${e.target.tagName}`);
  }
});
#container {
  border: 1px solid #000;
  padding: 10px;
}

#container > div {
  display: inline-block;
  border: 1px solid #CCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
  <div>
    <h1>Element 1</h1>
    <p>Lorem ipsum...</p>
  </div>
  <div>
    <h1>Element 2</h1>
    <p>...dolor sit amet...</p>
  </div>
</div>

Note that the CSS in the above example is just to make the bounds of each element more visible.

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!