-3

I have made this javascript code, but i think i am repeating my self, so any suggestions to make this code more better and optimized.

var countHeight = [];
$('.box').each(function() {
    countHeight.push( $(this).outerHeight() );
});
var maxValueInArray = Math.max.apply(Math, countHeight);

$('.box').each(function() {

  $(this).css('height', maxValueInArray+'px');

});

2 Answers2

0

This is one set of optimization i can think of

$(function(){

var maxHeight = 0;
$('.box').each(function() {
    var h = $(this).outerHeight();
    if(h>maxHeight) maxHeight=h;
});

$('.box').each(function() {
  $(this).css('height', maxHeight+'px');
});

});

Fiddle

void
  • 36,090
  • 8
  • 62
  • 107
0

Here is slightly shorter version. No need to use second loop to add the height to each .box element.

var countHeight = [];
$('.box').each(function() {
    countHeight.push( $(this).outerHeight() );
}).css('height', Math.max.apply(Math, countHeight)+'px');
Lajon
  • 311
  • 1
  • 9