Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 695 characters in body
Source Link
showdev
  • 29.3k
  • 37
  • 61
  • 82

To reiterate my comments:

  1. Resetting curTallest on each iteration will prevent finding the tallest element. Each element in the loop will be considered the tallest because curTallest is reset to zero each time.

  2. You'll only need to reset the height of divGroup if current.height() > currTallest. Currently, you reset the heights upon each iteration, regardless of whether currTallest has changed.

  3. cardHeights() expects a jQuery object. You are passing it a string. Either pass a jQuery object or convert the string to an object within the function.


That being said, my suggestion is to collect all heights, determine the maximum height from those values, and set all heights to the maximum height. This prevents needlessly setting heights multiple times.

Here's an example:

$(function() { cardHeights('.card'); $(window).on('resize', cardHeights('.card')); }); function cardHeights(divGroup) { /* Initialize variables */ var heights = [], $group = $(divGroup); /* Iterate through the selected elements, pushing their heights into an array */ $group.each(function() { heights.push($(this).height()); }); /* Determine the maximum height in the array */ var maxHeight = Math.max.apply(Math, heights); /* Set the height of all elements in the group to the maximum height */ $group.height(maxHeight); }
div.card { background-color: #CCC; margin: 1em; padding: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="card">Test</div> <div class="card">Test<br />Tester<br />Testing<br />Test-o-rama<br />Tallest!</div> <div class="card">Test<br />Tester</div>

Edit

If, for some reason, you don't want to use an array, you can use your original method of comparing each element's height to the maximum height:

function cardHeights(divGroup) { /* Initialize variables */ var $group = $(divGroup), maxHeight=0, thisHeight=0; /* Iterate selected elements, reset maxHeight if the current height is taller */ $group.each(function() { thisHeight=$(this).height(); if (thisHeight>maxHeight) {maxHeight=thisHeight;} }); /* Set the height of all elements in the group to the maximum height */ $group.height(maxHeight); } 

To reiterate my comments:

  1. Resetting curTallest on each iteration will prevent finding the tallest element. Each element in the loop will be considered the tallest because curTallest is reset to zero each time.

  2. You'll only need to reset the height of divGroup if current.height() > currTallest. Currently, you reset the heights upon each iteration, regardless of whether currTallest has changed.

  3. cardHeights() expects a jQuery object. You are passing it a string. Either pass a jQuery object or convert the string to an object within the function.


That being said, my suggestion is to collect all heights, determine the maximum height from those values, and set all heights to the maximum height. This prevents needlessly setting heights multiple times.

Here's an example:

$(function() { cardHeights('.card'); $(window).on('resize', cardHeights('.card')); }); function cardHeights(divGroup) { /* Initialize variables */ var heights = [], $group = $(divGroup); /* Iterate through the selected elements, pushing their heights into an array */ $group.each(function() { heights.push($(this).height()); }); /* Determine the maximum height in the array */ var maxHeight = Math.max.apply(Math, heights); /* Set the height of all elements in the group to the maximum height */ $group.height(maxHeight); }
div.card { background-color: #CCC; margin: 1em; padding: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="card">Test</div> <div class="card">Test<br />Tester<br />Testing<br />Test-o-rama<br />Tallest!</div> <div class="card">Test<br />Tester</div>

To reiterate my comments:

  1. Resetting curTallest on each iteration will prevent finding the tallest element. Each element in the loop will be considered the tallest because curTallest is reset to zero each time.

  2. You'll only need to reset the height of divGroup if current.height() > currTallest. Currently, you reset the heights upon each iteration, regardless of whether currTallest has changed.

  3. cardHeights() expects a jQuery object. You are passing it a string. Either pass a jQuery object or convert the string to an object within the function.


That being said, my suggestion is to collect all heights, determine the maximum height from those values, and set all heights to the maximum height. This prevents needlessly setting heights multiple times.

Here's an example:

$(function() { cardHeights('.card'); $(window).on('resize', cardHeights('.card')); }); function cardHeights(divGroup) { /* Initialize variables */ var heights = [], $group = $(divGroup); /* Iterate through the selected elements, pushing their heights into an array */ $group.each(function() { heights.push($(this).height()); }); /* Determine the maximum height in the array */ var maxHeight = Math.max.apply(Math, heights); /* Set the height of all elements in the group to the maximum height */ $group.height(maxHeight); }
div.card { background-color: #CCC; margin: 1em; padding: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="card">Test</div> <div class="card">Test<br />Tester<br />Testing<br />Test-o-rama<br />Tallest!</div> <div class="card">Test<br />Tester</div>

Edit

If, for some reason, you don't want to use an array, you can use your original method of comparing each element's height to the maximum height:

function cardHeights(divGroup) { /* Initialize variables */ var $group = $(divGroup), maxHeight=0, thisHeight=0; /* Iterate selected elements, reset maxHeight if the current height is taller */ $group.each(function() { thisHeight=$(this).height(); if (thisHeight>maxHeight) {maxHeight=thisHeight;} }); /* Set the height of all elements in the group to the maximum height */ $group.height(maxHeight); } 
Source Link
showdev
  • 29.3k
  • 37
  • 61
  • 82

To reiterate my comments:

  1. Resetting curTallest on each iteration will prevent finding the tallest element. Each element in the loop will be considered the tallest because curTallest is reset to zero each time.

  2. You'll only need to reset the height of divGroup if current.height() > currTallest. Currently, you reset the heights upon each iteration, regardless of whether currTallest has changed.

  3. cardHeights() expects a jQuery object. You are passing it a string. Either pass a jQuery object or convert the string to an object within the function.


That being said, my suggestion is to collect all heights, determine the maximum height from those values, and set all heights to the maximum height. This prevents needlessly setting heights multiple times.

Here's an example:

$(function() { cardHeights('.card'); $(window).on('resize', cardHeights('.card')); }); function cardHeights(divGroup) { /* Initialize variables */ var heights = [], $group = $(divGroup); /* Iterate through the selected elements, pushing their heights into an array */ $group.each(function() { heights.push($(this).height()); }); /* Determine the maximum height in the array */ var maxHeight = Math.max.apply(Math, heights); /* Set the height of all elements in the group to the maximum height */ $group.height(maxHeight); }
div.card { background-color: #CCC; margin: 1em; padding: 1em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="card">Test</div> <div class="card">Test<br />Tester<br />Testing<br />Test-o-rama<br />Tallest!</div> <div class="card">Test<br />Tester</div>