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

216
Visualizações
how to change the source of an image, which is in the innerhtml of another element?

i have a button with an image in its innerhtml and i want to change this image

<button id="button1"><img src="someimage.png"></button>

i tried something like this (in a function, which is called by the button):

this.innerhtml.setAttribute("src","someotherimage.png");
this.innerhtml="<img src="someotherimage.png">";

i have multiple buttons which trigger the same function, but i want the function to only change the img of the button, which triggered the function. Can i do this without giving the image an ID and using a buttload of if-statements to match the image-id to the button?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

With the event parameter, take the currentTarget (the element the listener is attached to - which will be the button) and from there you can get the child <img> with .children[0].

.addEventListener('click', (e) => {
  e.currentTarget.children[0].src = 'someotherimage.png';
});
about 4 years ago · Juan Pablo Isaza Relatório

0

More specific to img tag

.addEventListener('click', ($event) => {
    event.target.querySelector('img').src="someotherimage.png";
 });
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