Enrique Enrique - 29 days ago 10
CSS Question

Get height of div containing image with js offsetHeight

I'm trying to get the height of a div containing a headline and an image. All I get back is the height of the heading. If I add a specific height to the container div I get the correct height (in this case 300px), but I need the different heights. Thx for any help!

My js code:

var tile1 = document.getElementById('t1').offsetHeight;


My HTML:

<div class="t" id="t1" style="height:300px !important;">
<article>
<header class="beitrag">
<div class="beitrag-meta">Date: 2016-11-02 19:59:45</div>
</header>
<div class="beitrag-inhalt"><img src="219594520163877.jpg" ></div>
</article>
</div>

Answer

Make sure you runt it after the page has been loaded.

window.addEventListener('load', function() {
  var tile1 = document.getElementById('t1').offsetHeight;
  alert(tile1);
})
<div class="t" id="t1">
   <article>
      <header class="beitrag">
         <div class="beitrag-meta">Date: 2016-11-02 19:59:45</div>
      </header>
      <div class="beitrag-inhalt"><img src="http://placehold.it/100/"></div>
  </article>
</div>

Comments