Aras
Aras

Reputation: 497

make all parent divs as tall as their own children using jquery

I have a number of position absolute parent divs and some child elements in each of them, they are not identical at all but they all have the same class "force-middle" What I want to do is to calculate the total height of each div's children and apply it to the parent div

what I have so far is this:

var total = 0;

$(".force-middle").each(function() {
   $(this).children().each(function() {
        total = total + $(this).outerHeight();
    });
    $(this).css({"height":total});
});

Thanks in advance

Upvotes: 0

Views: 37

Answers (1)

Waseem Bepari
Waseem Bepari

Reputation: 336

initialize total variable each for a parent like this

var total = 0;
$(".force-middle").each(function() {
 total = 0
 $(this).children().each(function() {
    total = total + $(this).outerHeight();
 });
 $(this).css({"height":total});
});

Upvotes: 1

Related Questions