Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
How to alter img src onclick using jQuery or JS

I have multiple images with src URLs that all have

....content/..._gal6.jpg?format=100w

How can I use jQuery or JS to change all URLs to end with

?format=2500w

on click?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Like this

document.querySelectorAll("img").forEach(img => {
  const url = new URL(img.src);
  if (url.searchParams.get("format")) {
    url.searchParams.set("format", "2500w")
    console.log(url.toString())
    img.src = url.toString()
  }  
})
<img src="bla.jpg?format=250w">
<img src="bla.jpg">
<img src="bla.jpg?format=350w">

about 4 years ago · Santiago Gelvez Denunciar

0

Using Javascript:

Change the src property of the image:

function changeImage(){
document.getElementById('test-image').src = "https://url-to-second-image.com/image.png";
}

You can call the function using the click event in the html:

    <img id="test-image" src="the-url/first_image.jpg" onclick="changeImage()">

Here is a jsfiddle: https://jsfiddle.net/f5mwext3/2/

The same way you can change only part of the name (I'm using whole external files for convenience)

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda