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

154
Views
why isn't clientHeight 150px?

let height = document.getElementById('d').clientHeight
console.log(height);
img{
height:100%;}
<div id = 'd'>
<img src="https://via.placeholder.com/100x150">
</div>

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

0

The image hasn't loaded when you are console logging. You can create an event listener that waits for the image to be loaded and then it will show the proper height.

function loaded() {
    let height = document.getElementById('d').clientHeight;
    console.log(height);
}

const img = document.getElementById('img');
if (img.complete) {
  loaded()
} else {
  img.addEventListener('load', loaded)
}
img{
height:100%;}
<div id = 'd'>
<img id='img' src="https://via.placeholder.com/100x150">
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Because the image hasn't loaded when your code is executing:

setTimeout(() => {
    let height = document.getElementById('d').clientHeight
    console.log(height);
}, 1000)
<div id = 'd'>
<img src="https://via.placeholder.com/100x150">
</div>

If your code depends on knowing the height of the image in order to run correctly, you need to check whether the image is loaded, and wait until it has if it hasn't yet.

about 4 years ago · Juan Pablo Isaza Report

0

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded, which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

addEventListener('load',(event)=>{
    let height = document.getElementById('d').clientHeight
    console.log(height);
});
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!