skip to Main Content

I am having some issues with apply jQuery code on multiple classes with the same class name on a page. It looks like the code will only work for one of the class but not for more than one class.

As you can see there are two TwoColumnUnit classes on the page but only the first TwoColumnUnit is picking up the jQuery code but not picking up the second TwoColumnUnit class.

How can I fix the code so that, this code will work on multiple classes with the same class name?

$('.TwoColumnUnit').each(function() {
  var pairs = [document.querySelector('.TwoColumnUnit_Left'), document.querySelector('.TwoColumnUnit_Right')];
  pairs.forEach(function(pair) {
    if (pair) {
      var boxes = pair.getElementsByClassName('col-sm-12 col-md-6');
      var maxHeight = 0;
      Array.from(boxes).forEach(function(box) {
        box.style.height = ''; // Reset
        maxHeight = Math.max(maxHeight, box.offsetHeight);
      });
      Array.from(boxes).forEach(function(box) {
        box.style.height = maxHeight + 'px';
      });
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="TwoColumnUnit">
  <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-route"></i>
          <h3>This is about Tennis</h3>
        </div>
        <div class="right_Side">
          <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
        </div>
      </div>
    </div>
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-basketball"></i>
          <h3>This is about Basketball</h3>
        </div>
        <div class="right_Side">
          <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
        </div>
      </div>
    </div>
  </div>
  <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-basketball"></i>
          <h3>This is about Basketball</h3>
        </div>
        <div class="right_Side">
          <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
        </div>
      </div>
    </div>
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-route"></i>
          <h3>This is about Tennis</h3>
        </div>
        <div class="right_Side">
          <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
        </div>
      </div>
    </div>
  </div>
</div>



<div class="TwoColumnUnit">
  <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-route"></i>
          <h3>This is about Tennis</h3>
        </div>
        <div class="right_Side">
          <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
        </div>
      </div>
    </div>
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-basketball"></i>
          <h3>This is about Basketball</h3>
        </div>
        <div class="right_Side">
          <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
        </div>
      </div>
    </div>
  </div>
  <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-basketball"></i>
          <h3>This is about Basketball</h3>
        </div>
        <div class="right_Side">
          <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
        </div>
      </div>
    </div>
    <div class="newsLetterDiv">
      <div class="col-sm-12 col-md-6">
        <div class="left_Side">
          <i class="fas fas fa-route"></i>
          <h3>This is about Tennis</h3>
        </div>
        <div class="right_Side">
          <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
        </div>
      </div>
    </div>
  </div>
</div>

2

Answers


  1. querySelector() will only select the first occurrence in .TwoColumnUnit. You should use querySelectorAll() instead.

    Also, I will suggest you to avoid mixing JS and jQuery.

    Demo:

    $('.TwoColumnUnit').each(function () {
        var pairs = [$(this).find('.TwoColumnUnit_Left'), $(this).find('.TwoColumnUnit_Right')];
        pairs.forEach(function (pair) {
            if (pair.length > 0) {
                var boxes = pair.find('.col-sm-12.col-md-6');
                var maxHeight = 0;
                boxes.each(function () {
                    $(this).css('height', ''); // Reset
                    maxHeight = Math.max(maxHeight, $(this).height());
                });
                boxes.css('height', maxHeight + 'px');
            }
        });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="TwoColumnUnit">
        <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-route"></i>
                        <h3>This is about Tennis</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
                    </div>
                </div>
            </div>
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-basketball"></i>
                        <h3>This is about Basketball</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">    
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-basketball"></i>
                        <h3>This is about Basketball</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
                    </div>
                </div>
            </div>
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-route"></i>
                        <h3>This is about Tennis</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    
    
    <div class="TwoColumnUnit">
        <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-route"></i>
                        <h3>This is about Tennis</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
                    </div>
                </div>
            </div>
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-basketball"></i>
                        <h3>This is about Basketball</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">    
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-basketball"></i>
                        <h3>This is about Basketball</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
                    </div>
                </div>
            </div>
            <div class="newsLetterDiv">
                <div class="col-sm-12 col-md-6">
                    <div class="left_Side">
                        <i class="fas fas fa-route"></i>
                        <h3>This is about Tennis</h3>
                    </div>
                    <div class="right_Side">
                        <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    Login or Signup to reply.
  2. Because your DOM selectors pick up the same two divs

    You want to use jQuery as much as possible or none at all

    Note I do not need to test the existence of the divs since the script will ignore them if they are not found

    $('.TwoColumnUnit').each(function() {
      const $boxes = $(this).find('.newsLetterDiv > div'); // the divs right under newsLetter
      var maxHeights = $boxes
        .map(function() { // jQuery map
          return +this.offsetHeight; // the DOM property
        })
        .get(); // array
      const maxHeight = Math.max(...maxHeights); // find the tallest
      $boxes.each(function() {
        $(this).css({ "height": `${maxHeight}px` });
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <div class="TwoColumnUnit">
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
      </div>
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    
    
    <div class="TwoColumnUnit">
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
      </div>
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
      </div>
    </div>

    No jQuery

    document.querySelectorAll('.TwoColumnUnit').forEach(unit => {
      const boxes = unit.querySelectorAll('.newsLetterDiv > div'); // the divs right under newsLetter
      const maxHeights = [...boxes].map(box => +box.offsetHeight);
      const maxHeight = Math.max(...maxHeights); // find the tallest
      boxes.forEach(box => box.style.height = `${maxHeight}px`);
    });
    <div class="TwoColumnUnit">
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
      </div>
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    
    
    <div class="TwoColumnUnit">
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Left">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
      </div>
      <div class="col-md-6 col-xs-12 TwoColumnUnit_Right">
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-basketball"></i>
              <h3>This is about Basketball</h3>
            </div>
            <div class="right_Side">
              <p><span>Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.Basketball is a team sport in which two teams, most commonly of five players each, opposing one another on a rectangular court, compete with the primary objective of shooting a basketball through the defender's hoop, while preventing the opposing team from shooting through their own hoop.</span></p>
            </div>
          </div>
        </div>
        <div class="newsLetterDiv">
          <div class="col-sm-12 col-md-6">
            <div class="left_Side">
              <i class="fas fas fa-route"></i>
              <h3>This is about Tennis</h3>
            </div>
            <div class="right_Side">
              <p><span>Tennis is a racket sport that is played either individually against a single opponent or between two teams of two players each.&nbsp;</span></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search