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

272
Views
how to add event listener to li thats inside a div

I have been trying to add an event listener to three li elements. The issue is that doing document.getElementbyid(about) doesn't work. But when I do get element document.getElementbyid(last), it works but on all three elements simultaneously.

I want to target them individually so that it opens up models respective to each li element.

Here is the HTML:

<section>
    <h1 id="intro">Hi, My name is</h1>
   <div id="last">
      <ul>
        <li id="about">About</li>
        <li id="project">Projects</li>
        <li id="resume">Resume</li>
      </ul>
   </div>
</section> 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

well like this :

let lis = document.querySelectorAll('section div ul li')
lis.forEach((li) => {
    li.addEventListener('click', () => {

    })
})

if you want you can handle each li individually like so :

let resume = document.querySelector('#resume')
resume.addEventListener('click', () => {
    // do whatever you need
})

the click event here is an exemple, use the event you need, like perhaps mouseover

about 4 years ago · Juan Pablo Isaza Report

0

It is possible to add a click event to each element. You would use querySelectorAll to reference them all. You would then loop over it.

document.querySelectorAll("#last li").forEach(function (li) {
  li.addEventListener("click", function () {
    console.log("Clicked", li.id);
  });
});

A better option is event delegation where you just bind one event.

Here is the basic idea using event delegation. Add one click event listener on a parent. You select the target which is what was clicked and find the link. You can use the link's hash to get the element you want to show.

// Bind one click event to the parent, we will use event delegation
document.getElementById("last").addEventListener("click", function(event) {
  // get what was clicked and look for the anchor tag
  const anchor = event.target.closest("a");
  // did we find an anchor?
  if (anchor) {
    // hash will have the id of the element to show
    showElement(anchor.hash);
  }
});

function showElement(selector) {
    // do we have an active modal? Yes? Close it
    const modalActive = document.querySelector(".modal.active");
    if (modalActive) {
      modalActive.classList.remove('active');
    }

    // Enable the modal we just clicked on
    const elem = document.querySelector(selector);
    if (elem) {
      elem.classList.add('active');
    }
}

// Page load, if has hash then show that element
const hash = window.location.hash;
if (hash) {
  showElement(hash);
}
.modal {
  display: none;
}

.active {
  display: block;
}
<section>
  <h1 id="intro">Hi, My name is</h1>
  <div id="last">
    <ul>
      <li><a href="#about">About</a></li>
      <li><a href="#project">Projects</a></li>
      <li><a href="#resume">Resume</a></li>
    </ul>
  </div>
</section>

<div id="about" class="modal">ABOUT CONTENT</div>
<div id="project" class="modal">PROJECT CONTENT</div>
<div id="resume" class="modal">RESUME CONTENT</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!