I have a grid that lists products. The grid is three columns wide, but it could grow to be four or five columns wide. I've written my script to count the columns. The markup is something like this:
<ul>
<li class="item first">...</li>
<li class="item">...</li>
<li class="item">...</li>
<li class="item first">...</li>
<li class="item">...</li>
<li class="item">...</li>
</ul>
In the project, I can't change the markup. I can however write JavaScript/jQuery any which way I please. What I have is working, but I'm using four loops. Since my JS chops aren't too hot, I'm not convinced that what I have is particularly fast or concise.
jQuery(document).ready(function ($) {
"use strict";
var maxItemHeight,
firstItem = $('.item.first'),
gridRow = [],
i;
firstItem.each(function () {
gridRow.length = 0;
maxItemHeight = 0;
gridRow.push($(this));
$(this).nextUntil(firstItem).each(function () {
gridRow.push($(this));
});
for (i = 0; i < gridRow.length; i++) {
maxItemHeight = (gridRow[i].find('h2').outerHeight() > maxItemHeight) ? gridRow[i].find('h2').outerHeight() : maxItemHeight;
};
for (i = 0; i < gridRow.length; i++) {
gridRow[i].find('h2').css('height', maxItemHeight);
};
});
});
How could this be improved?