views:

216

answers:

3

If i have the following markup;

<div id="container">
  <div id="box">
    <div id='sameHeight'>One<br>two<br>three</div>
    <div id='sameHeight'>four</div>
    <div id='sameHeight'>five</div>        
  <div>
  <div id="box">
    <div id='sameHeight'>four</div>
    <div id='sameHeight'>six</div>
    <div id='sameHeight'>seven<br>eight</div>
  <div>
</div>

How can I ensure that all divs marked as "sameHeight" are the same height as their counterparts in the other div?

I had a look at the equalHeights plugin but that assumes all divs side by side are in the same parent. I need one that can either traverse parents or allow me to specify parents.

Is there such a thing or do I need to write it?

EDIT

I seem to have caused some confusion in my explanation so I hope this clears things up a little.

Looking at the new markup, the container is a simple box.

The "box" divs sit side by side.

Each sameheight div then sits one under the other within its parent.

The thing i'm trying to solve is to have each of the sameHeights that match to it's opposite side the same height.

it should look like a grid i guess w/out using a grid.

I hope this helps.

EDIT 2

This is so far what I have come up with but is there a better way?

function SetHeights() {
    var numLines = $('#container>div:eq(0) .sameHeight').length;

    for (var t = 0; t < numLines; t++) {
        var leftHeight = $('#container>div:eq(0) .sameHeight:eq(' + t + ')').outerHeight();
        var rightHeight = $('#container>div:eq(1) .sameHeight:eq(' + t + ')').outerHeight();

        if (leftHeight > rightHeight) {
            $('#container>div:eq(1) .sameHeight:eq(' + t + ')').css({ height: leftHeight });
        }
        else {
            $('#container>div:eq(0) .sameHeight:eq(' + t + ')').css({ height: rightHeight });
        }
    }
}
A: 
<div id='parentDiv'>
  <div>
      <div id='sameHeight'>One<br>two<br>three</div>
      <div id='sameHeight'>four</div>
      <div id='sameHeight'>five</div>        
  <div>
  <div>
      <div id='sameHeight'>four</div>
      <div id='sameHeight'>six</div>
      <div id='sameHeight'>seven<br>eight</div>
  <div>
</div>

and in JavaScript using jQuery write :

$(document).ready(function () {    
    $("div.parentDiv div div.sameHeight").css({ height: "100px" });
});
Sumit Sharma
Ah, thanks but I sould mention that I can't assume a height. Each 'sameHeight' may be different and so i need to set the height of each to that of the highest one in that line.
griegs
+3  A: 

Firstly, you are probably aware only one element should have any one id attribute. So I have changed the selectors as if they were classes to classes below. Even though you may not give a care about W3C standards, browsers or JavaScript API, etc may rely on this behaviour and not work now or in the future.

   $(document).ready(function () {    
        $('div.parentDiv > div').each(function() {

        var $sameHeightChildren = $(this).find('.sameHeight');
        var maxHeight = 0;

        $sameHeightChildren.each(function() {
            maxHeight = Math.max(maxHeight, $(this).outerHeight());
        });

        $sameHeightChildren.css({ height: maxHeight + 'px' });


    });
});

Note: If you want them all to be the same height despite their parent div, this is what you want.

$(document).ready(function () {    
        var $sameHeightDivs = $('.sameHeight');
        var maxHeight = 0;
        $sameHeightDivs.each(function() {

            maxHeight = Math.max(maxHeight, $(this).outerHeight());

        });

        $sameHeightDivs.css({ height: maxHeight + 'px' });
});

This will set them to all be the height of the tallest, per parent div element.

Also, this answer may show you some other options.

Note too that if the content inside changes again (perhaps via JavaScript), you will need to call this again, or put it in a setInterval() which I do not recommend.

alex
Agreed, this is the way to do it in JQuerybut you could do it simply in CSS by setting the height of the child divs to 100%
AjayP
@AjayP You can not actually. This is the whole *equal heights problem* in CSS, and where the table people have it easy :P
alex
... I mean without a fixed height of course. The comment to the answer by Sumit Sharma says "I can't assume a height".
alex
Really I always thought 100% would work I've never had to do this though.
AjayP
Well maybe I'm wrong. Show it to me working on www.jsbin.com and I'll eat my words :)
alex
Alex, at the risk of sounding lazy and pedantic, can you adjust your code so that it checks the height of both sides? at present it works if the left side is > than the right so +1 on that but I like to have it set the height of either the left or right depending on which opposite side it higher. again, being lazy. :)
griegs
Ha your right:http://jsbin.com/iwejo
AjayP
@griegs I get what you want I think. See my (soon to come) edit.
alex
Um, also each line may be different. presently your code sets the hight of all divs to the height of the largest div. i need that behaiviour on a per sameHeight basis.
griegs
@griegs Is the update what you wanted?
alex
@griegs I think maybe now I confused what you wanted. Are you able to draw a quick image of what you want, a mockup in CSS or some ASCII art?
alex
I think, by looking at it, it's still setting all the sameHeights to the highest of them. I kinda need each line to be set to the highest within that line. so need to loop through the left hand line and check against the right line and set height of whichever is highest then do the same for the next line.
griegs
@griegs It sounds like you are talking about what it looks like with CSS applied, so it's a bit confusing for me (sorry!). Do you have a test page you can show, CSS you can post or can you set up a quick testbed on www.jsbin.com?
alex
I edited my question which i hope helps.
griegs
@griegs See my second code snippet. See where it says `$('.sameHeight')`? Change that argument to a CSS selector that matches your boxes. In your example markup, that would be `$('#box')`. However using the same id attribute over multiple elements on the same page can only add to more possible problems. Can we change your sample markup to classes please?
alex
the number of lines is variable but they match up side by side. so we may have 10 lines or 3 lines but each side will have the same number of lines. so you can change them to classes if you wish. I have done some code in my second edit. can anyone see an improvement to this?
griegs
Are you able to extract the relevant CSS and HTML and post it on to www.jsbin.com? Then I can directly apply my JavaScript
alex
A: 

Why not just use a table?

Imho, doing layout in javascript like this is far more evil than doing the layout via tables. What happens if the user resizes their browser? You have to capture the resize event, but then you get a certain laggyness when resizing that makes your website look unpolished.

kwyjibo
I agree somewhat with this. However, I wouldn't use a table, just cheat with the `margin-bottom: -1000px; padding-bottom: 1000px; overflow: hidden;` trick.
alex