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

178
Views
using event.target to get the button id its not working

I have three buttons with images and I'm trying to get the ID of the clicked one, but when i click in the button (with an image) it just return the ID of the image, not the button (sometimes return the ID of the button too). There's something that im missing? Or another way to do that? please, help!

Here is my code:

buttons = document.querySelectorAll('button');
buttons.forEach((btn) => btn.addEventListener("click", (e) => {
    let userChoice = e.target.id;
    console.log(userChoice);
}));
<button id="myBtn1"><img src="images/img1.png"></button>
<button id="myBtn2"><img src="images/img2.png"></button>
<button id="myBtn3"><img src="images/img3.png"></button>

If you want to test, just run the code and click in the buttons. (sorry if i'm not clear, i'm new here).

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

0

Please replace:

e.target.id with e.currentTarget.id

current target always refers to the element to which the event handler has been attached, as opposed to Event.target, which identifies the element on which the event occurred and which may be its descendant.

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!