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

180
Views
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
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!