0

I am trying to make one div use the same height as another.

I am fairly new to javascript and have come up with this to set both heights to be the same.

    /* Get height of div within the right column */
    var innerheight = $(".innerdiv").height(); 
    /* Get height of left column */
    var left = $(".leftcolumn").height(); 

    /* Set height of right column to left on 
larger screens else stay same as height as it's child inner div*/
    if ($(window).width() > 870) {
    $(".rightcolumn").css({ height: leftheight });
    }
    else 
    {
    $('.rightcolumn').css({ height: innerheight });
    }

So I was getting strange results, most of the time it worked fine but occasionally the height would be random.

document.getElementById("height").innerHTML = leftheight;

With this I am getting the same correct value 80% of time but sometimes random values?

What would be causing this? The left column loads images in to it, is there a possible delay loading and it's getting the wrong value? Any way around this?

This question may already have an answer here: How to add onload event to a div element? 16 answers

This isn't what I'm looking for.

  • Can you share a live example. Specially with the content of each div. – Gerardo BLANCO Apr 11 '18 at 00:31
  • 1
    Make sure your Element contents are loaded first. This may require a `load` Event. – StackSlave Apr 11 '18 at 00:37
  • @PHPglue do you mean the order? The html (both columns) are before / above the javascript, could you explain how I would do a load event? Would it need a delay? –  Apr 11 '18 at 00:39
  • @GerardoBLANCO the left div contains a different amount of images on each page, the right div contains a smaller child div that is position sticky. –  Apr 11 '18 at 00:42
  • You cant put an onload event for a div – Gerardo BLANCO Apr 11 '18 at 00:47
  • 1
    Possible duplicate of [How to add onload event to a div element?](https://stackoverflow.com/questions/4057236/how-to-add-onload-event-to-a-div-element) – Gerardo BLANCO Apr 11 '18 at 00:48
  • I'm not trying to add an onload event I'm trying to get a consistent value of height... –  Apr 11 '18 at 00:50
  • If there's an image being generated, you would do `imageElement.onload = function(){ /* do your other stuff here */ }` or `imageElement.addEventListener('load', function(){ /* do your other stuff here */ });`. – StackSlave Apr 11 '18 at 00:53

0 Answers0