skip to Main Content

I have created a function that is supposed to check the size of each element store the biggest size in a variable, then apply that size to all the elements. It seems to be working when the browser is refreshed but not when the window is resized. Here’s my code;

$(document).ready(function() {
  function jobs_height() {
    var highest = 0;

    $(".jobs").each(function() {
      var job_height = $(this).outerHeight(true);

      if (job_height > highest) {
        highest = job_height;
      }
    });

    $(".jobs").css("height", highest);
  }

  //calling functions 
  $(window).ready(function() {
    jobs_height();

    $(window).resize(function() {
      jobs_height();
    });
  });
});
.jobs {
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container" id="jobs_cont">
  <div class="row">

    <div class="col-xs-12 col-sm-4">
      <div class="jobs">
        <h2 class="job_title">Construction Site<br>Supervisor role</h2>
        <p class="salary">Salary: £20,000 – £22,000</p>
        <p class="job_info">Our client is a well-established and respected Building and Refurbishment company based in Leeds, are looking to add an experienced Construction Site Supervisor to their team. <a href="#">See More</a></p>
        <a href="#" class="red_button cv_button">SUBMIT CV</a>
      </div>
    </div>

    <div class="col-xs-12 col-sm-4 col-sm-4">
      <div class="jobs">
        <h2 class="job_title">Construction Contracts<br>Manager role</h2>
        <p class="salary">Salary: £40,000 – £45,000</p>
        <p class="job_info">Our client is a well-established and respected Building and Refurbishment company based in Leeds, are looking to add an experienced Construction Contracts Manager to their...<a href="#">See More</a></p>
        <a href="#" class="red_button cv_button">SUBMIT CV</a>
      </div>
    </div>

    <div class="col-xs-12 col-sm-4">
      <div class="jobs">
        <h2 class="job_title">Graduate Quantity<br>Surveyor role</h2>
        <p class="salary">Salary: £20,000 – £22,000</p>
        <p class="job_info">Our client a well-established and respected Building and Refurbishment company based in Leeds, are looking to add a Graduate Quantity Surveyor to their team. <a href="#">See More</a></p>
        <a href="#" class="red_button cv_button">SUBMIT CV</a>
      </div>
    </div>

  </div>
</div>

https://codepen.io/Reece_Dev/pen/aLXjbz

UPDATE:

after trying Jameson the dog’s and Simon’s answers which both work I have encountered another problem. When I hard refresh i get this

new issue

as you can see the boxes are too small. but once I resize the window it works fine. Why is this happening?

UPDATE 2:

Ok so I figured out why it wasn’t working on load. I was because I was using the wrong function to check when the page was loaded. What I should of been using is;

jQuery(window).on('load', function(){
    jobs_height();
});

2

Answers


  1. think about in a flow manner.

    first you set the height of all the elements to a fixed height.

    then you try to query the elements height – you will always get the same height you’ve set before! (because it’s fixed and “hard coded”)

    just add this line $(".jobs").css("height", ''); in the start of jobs_height() to cancel your previous definition (it happens so fast you won’t notice) and you’re golden

    EDIT

    simon is right, once document ready is called window ready won’t be called – so you should just call jobs_height() instead

    $(document).ready(function() {
      function jobs_height() {
        $(".jobs").css("height", '');
        var highest = 0;
    
        $(".jobs").each(function() {
          var job_height = $(this).outerHeight(true);
    
          if (job_height > highest) {
            highest = job_height;
          }
        });
    
        $(".jobs").css("height", highest);
      }
    
      //calling functions 
      jobs_height(); // call function instead of adding listener 
      $(window).resize(jobs_height);
     
    });
    .jobs {
      background-color: yellow;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="container" id="jobs_cont">
      <div class="row">
    
        <div class="col-xs-12 col-sm-4">
          <div class="jobs">
            <h2 class="job_title">Construction Site<br>Supervisor role</h2>
            <p class="salary">Salary: £20,000 – £22,000</p>
            <p class="job_info">Our client is a well-established and respected Building and Refurbishment company based in Leeds, are looking to add an experienced Construction Site Supervisor to their team. <a href="#">See More</a></p>
            <a href="#" class="red_button cv_button">SUBMIT CV</a>
          </div>
        </div>
    
        <div class="col-xs-12 col-sm-4 col-sm-4">
          <div class="jobs">
            <h2 class="job_title">Construction Contracts<br>Manager role</h2>
            <p class="salary">Salary: £40,000 – £45,000</p>
            <p class="job_info">Our client is a well-established and respected Building and Refurbishment company based in Leeds, are looking to add an experienced Construction Contracts Manager to their...<a href="#">See More</a></p>
            <a href="#" class="red_button cv_button">SUBMIT CV</a>
          </div>
        </div>
    
        <div class="col-xs-12 col-sm-4">
          <div class="jobs">
            <h2 class="job_title">Graduate Quantity<br>Surveyor role</h2>
            <p class="salary">Salary: £20,000 – £22,000</p>
            <p class="job_info">Our client a well-established and respected Building and Refurbishment company based in Leeds, are looking to add a Graduate Quantity Surveyor to their team. <a href="#">See More</a></p>
            <a href="#" class="red_button cv_button">SUBMIT CV</a>
          </div>
        </div>
    
      </div>
    </div>
    Login or Signup to reply.
  2. If you update the window functions so that there’s only a single window.resize and set the height of each of the divs to auto before you get their outerHeight() then that should solve it.

    Here’s the updated function:

    $(document).ready(function(){
    
        function jobs_height(){
    
            var highest = 0;
    
            $(".jobs").each(function(){
    
                $(this).css('height','auto');
                var job_height = $(this).outerHeight();
    
                if(job_height > highest){
                    highest = job_height;
                }
    
            });
    
            $(".jobs").css("height", highest);
        }
    
        //calling functions 
        $(window).resize(jobs_height);
    
    });
    

    This can be tested on CodePen

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search