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

252
Views
javascript - get element that was clicked from collection of items

Having this code:

let ev = document.querySelectorAll('.event p')

for (let i = 0; i < ev.length; i++) {
    ev[i].addEventListener("click", function (e) {
        // add this only for current clicked elem
        e.currentTarget.src = './icon.png';
        // And the rest of items should have this:
        ev[i].src = './warning.png';
    });
}

How can i change warning.png to all elements in this loop, but change src for element to have icon.png that was clicked? So kind of toggle. My code is not working as expected. Thanks.

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

0

You can reuse ev inside the event listener, like this: ev.forEach(evt => evt.src = './warning.png');

If the list of .event p changes, recalculate the list again, e.g. put ev = document.querySelectorAll('.event p') inside the listener.

let ev = document.querySelectorAll('.event p')

for (let i = 0; i < ev.length; i++) {
    ev[i].addEventListener("click", function (e) {
        // Change the icon for all items
        ev.forEach(evt => evt.src = './warning.png');

        // then change the icon for the current item
        e.currentTarget.src = './icon.png';
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

The issue is that the statement ev[i].src = './warning.png'; within the for loop is overriding e.currentTarget.src = './icon.png'; . The solution here is changing all the elements within the for loop, and outside the loop change the icon ONLY for the targetted event.

let ev = document.querySelectorAll('button');
console.log(ev);

// Add event listener to each element
for (let i = 0; i < ev.length; i++) {
  ev[i].addEventListener('click', (e) => {
    for (let i = 0; i < ev.length; i++) {
    // Change icon for all the elements
    ev[i].src = './warning.png';
  }
  // Change icon only for targetted event
  e.target.src = './icon.png';
  });
}

Also, changed currentTarget to target since target is going to specifically get the element that triggered the event, whereas using currentTarget we can run into confusion in certain scenarios, because it will get the element that the event listener is attached to.

about 4 years ago · Juan Pablo Isaza Report

0

In your code after the click event on element, that element's src is changing but later on it is changing again to ./warning.png. Rearranging them will solve the problem.

let ev = document.querySelectorAll('.event p');
const evLen = ev.length;
for (let i = 0; i < evLen; i++) {
    ev[i].addEventListener("click", function (e) {
       ev[i].src = './warning.png'; 
       this.src = './icon.png';
    });
}
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!