2

I have the following construct in my HTML document:

<div class="content"> <section class="column"></section> <aside class="column"></aside> </div> 

And I want to match the heights of the section and aside to the height of the tallest of the two. I have the following script, but it's not working, any ideas:

 function unifyHeights() { var maxHeight = 0; $('div.content').children('.column').each(function(){ var height = $(this).outerHeight(); if ( height > maxHeight ) { maxHeight = height; } }); $('.column').css('height', maxHeight); } 
1
  • Did you test if that anonymous function is called twice, with the correct children? Did you look at the values height gets assigned? Does maxHeight increase? Commented Nov 11, 2010 at 22:48

3 Answers 3

1

Try using $('.column').css('height', maxHeight + 'px');

BTW, you can replace $('div.content').children('.column') with$('div.content > .column')

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like nothing was wrong with the function in your question, because both columns get the same height when using this markup and code (taken from your Pastebin example):

<!doctype HTML> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> <script> function unifyHeights() { var maxHeight = 0; $('div.content').children('.column').each(function(){ var height = $(this).outerHeight(); if ( height > maxHeight ) { maxHeight = height; } }); $('.column').css('height', maxHeight); } </script> <style> .content { background-color: yellow; } section.column { width: 300px; float: left; } aside.column { width: 300px; floaT: left; display: inline; } </style> </head> <body> <div class="content"> <section style="background-color: pink;" class="column">jldkjfls<br/><br/></section> <aside style="background-color: green;" class="column">jldkjfls<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/></section> <script> unifyHeights(); </script> </body> </html> 

Comments

0

A nice way to get the maximum of a set of properties is using .map():

$(document).ready(function() { var heights = $('div.content').children('.column').map(function(idx, el) { return $(this).outerHeight(); }).get(); $('.column').height(Math.max(heights)); }); 

3 Comments

I must be doing something fundamentally wrong since that doesn't work either - can you see in this test document what that might be: pastebin.com/JqfThuQ8
@Stuart: You're not invoking that function at all, and besides, according to my console and api.jquery.com, max is not a function.
@Marcel No, it isn't -- I thought it was a native JS array method. Editing now...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.