0

I have no knowledge at Javascript, but using some related stuff that I found while searching stackoverflow I came up with a code to make 2 divs the same height. I don't know if this was posted before but since I didn't find it, I want to know if there are problems of using this cross-browser and not CSS based answers.

document.getElementById('DIV1').style.height = document.getElementById('DIV2').offsetHeight + "px";

What this does is get the height from a div with the id DIV2 and set that height on div with the id DIV1.

(I didn't set vars because I think its not necessary for this simple code)

So, is there any problems using this?

EDIT:

<div class="row">
 <div class="col-xs-12 col-lg-8">
   <div id="DIV2">
      [image]
   </div>
 </div>
 <div class="col-xs-12 col-lg-4 text-center">
  <div id="DIV1" class="panel panel-primary">
   <div class="panel-body">
      [content]
   </div>
  </div>
 </div>
</div>

I added the code, sorry for not having it in the first post. So the image is bigger in height than the text that is in content.

1
  • In order to avoid guesswork, please include your html. Commented May 5, 2014 at 15:29

1 Answer 1

1

Doesn't look like a problem, IE supports the offsetHeight property. Just make sure you run the JavaScript after the divs have loaded, otherwise the styles might not be applied, because the div doesn't exist at the time the code runs. Such as load the scripts just before the closing body tag, or put them inside a function like this:

window.onload = function() {  /*code here*/ }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.