Tuesday, February 4, 2014

Make two divs equal height with jquery (in Drupal)

One problem I often face is that I have two divs, a content div and next to it another div with a menu, or links to related content or whatever. And I want both divs, f.i. #content and #rightbar, to be equal height, but they are added loosely to the html bacause the html needs to be responsive.  Sometimes you can get it working with css, but lots of time I get lost.
One solution is to simply compare the two and make them equal height with a javascript/jQuery function.


function equalHeight(one,two) {
   var tallest = 0;
   tallest=one.height();
   if (tallest < two.height()) {
      tallest=two.height();
   }
   one.height(tallest);
   two.height(tallest);

}


Call this function in a document ready like so

(function ($) {
// drupal jquery wrapper
     $(document).ready(function() {
        equalHeight($("#content"),$("#rightbar"));

     });

     function equalHeight(one,two) {
         var tallest 0;
         tallest=one.height();
         if (tallest two.height()) {
         tallest=two.height();
         }
         one.height(tallest);
         two.height(tallest);

         }
// emdof drupal jquery wrapper
}(jQuery));

No comments:

Post a Comment