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
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
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 ofjobs_height()
to cancel your previous definition (it happens so fast you won’t notice) and you’re goldenEDIT
simon is right, once
document ready
is calledwindow ready
won’t be called – so you should just calljobs_height()
insteadIf 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 theirouterHeight()
then that should solve it.Here’s the updated function:
This can be tested on CodePen