David Van Staden
David Van Staden

Reputation: 1779

Calculate height of 3 elements and find the biggest

good Day

I want to calculate the outerheight of 3 elements and return the biggest one, since their sizes can vary at any given time...

Here is what I have:

var a = $('#latestInner').outerHeight();
var b = $('#make').outerHeight();
var c = $('#models').outerHeight();

if (a > b, c) {
    a = x;
}
else if (b > a, c) {
    b = x;
}
else if (c > a, b) {
    c = x;
}

How would I proceed? I know I can probably use an array but not sure how to..

Upvotes: 0

Views: 134

Answers (2)

CD..
CD..

Reputation: 74096

Use Math.max:

Returns the largest of zero or more numbers.

Math.max([value1[,value2[, ...]]]) 

Using jquery you can do:

var max = Math.max.apply(null, $.map($('#latestInner, #make, #models'), function(n){ 
    return $(n).outerHeight(); 
}));

Upvotes: 5

dfsq
dfsq

Reputation: 193261

You can calculate max using each method:

var max = 0;
$('#latestInner, #make, #models').each(function() {
    max = Math.max(max, $(this).outerHeight());
});

http://jsfiddle.net/uwsJH/

If you need to update the height of the divs to be the same you can set it right away:

var max = 0;
$('#latestInner, #make, #models').each(function() {
    max = Math.max(max, $(this).outerHeight());
})
.height(max);

http://jsfiddle.net/uwsJH/1/

Upvotes: 2

Related Questions