Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda