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

183
Vistas
How to set img attributes with JS

So I am trying to set attributes (width and height) of my all images on page. So i do something like that:

const images = document.querySelectorAll('img');

const setImagesAttributes = () => {
    images.forEach(img => {
        img.setAttribute('width', img.clientWidth);
        img.setAttribute('height', img.clientHeight);
    })
}

window.onload = () => {
    setImagesAttributes();
    window.addEventListener('resize', debounce(setImagesAttributes));
}

And now something weird happen. So I think 90% of images work correct, the behave in same way like before adding script. But some images have very low height, idk what happend. Why one image behave normally, others not.

Before adding script: enter image description here

After script: enter image description here

But as i sad over 90% of images works correct. After adding script they have W & H attributes and work fine

about 4 years ago · Juan Pablo Isaza
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