tags:

views:

67

answers:

2

Hi, I want to match height of parent with the total height of its children, so that it's content does not flow out of its border. I'm trying following code :

 $("#leftcolumn").each(function(){
     totalHeight=totalHeight+$(this).height();
  });

Will it iterate through all the children of div ? Sometimes, it works sometime don't. Also, I tried following assuming that it will consider all its children, but the result is strange and it gives height double of the original.

 $("#leftcolumn > *").each(function(){
   totalHeight=totalHeight+$(this).height();
 });

Thanks in advance

+1  A: 

Try it like so:

$("#leftcolumn").children().each(function(){
    totalHeight = totalHeight + $(this).outerHeight();
});

http://api.jquery.com/outerHeight/ takes margins, paddings and borders into the calculation which should return a more reliable result.

jAndy
A: 
 $('#leftColumn').children().each(function(){
    var Totalheight += $(this).Height();
})var parentHeight = $('#leftColumn').Height();
if(parentHeight===TotalHeight)
{//Do the nasty part}
else
{//Do the Good part}
james