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

210
Views
¿Cómo cambiar la fuente de una imagen, que está en el HTML interno de otro elemento?

tengo un botón con una imagen en su HTML interno y quiero cambiar esta imagen

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

Intenté algo como esto (en una función, que se llama con el botón):

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

Tengo varios botones que activan la misma función, pero quiero que la función solo cambie la imagen del botón que activó la función. ¿Puedo hacer esto sin darle a la imagen una identificación y usar una gran cantidad de declaraciones if para hacer coincidir la identificación de la imagen con el botón?

Gracias por adelantado

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

0

Con el parámetro de event , tome el currentTarget (el elemento al que está conectado el oyente, que será el botón) y desde allí puede obtener el hijo <img> con .children[0] .

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

0

Más específico para la etiqueta img

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