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

181
Visualizações
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 à 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