0

I have a page with a few divs for which I wrote a script to adjust the height of it appropriately. Each div contains 2 divs in it and a small script is written to calculate the highest of these divs and lines them up correctly. The problem is that all the divs on the page are now assigned the same height. I would like it to calculate separately for each div.

HTML

  <article>
   <div class="inner-content-container equalHeight">
      some text....
   </div>
   <div class="entry-sidebar equalHeight"> some text...</div>
  </article>
  <article>
   <div class="inner-content-container equalHeight">
      some text....
   </div>
   <div class="entry-sidebar equalHeight"> some text...</div>
  </article>
  <article>
   <div class="inner-content-container equalHeight">
      some text....
   </div>
   <div class="entry-sidebar equalHeight"> some text...</div>
  </article>

jQuery

   var highestCol2 = Math.max(jQuery('.entry-sidebar').height(),jQuery('.inner-content-container').height());
   jQuery('.equalHeight', this).height(highestCol2);
6
  • you're not being clear here Commented Jul 2, 2014 at 2:28
  • You can use a callback function where you've got this in your jQuery call to specify a function that will affect each selected element individually. How you calculate the height individually is up to you. Please provide a fiddle if you want any further help. Commented Jul 2, 2014 at 2:29
  • Sorry for not being clear. I have multiple article divs who's height needs to be adjusted depending on the content inside it. At the moment the script is making all the article divs on the page the same height instead of calculating each one individually. Commented Jul 2, 2014 at 2:42
  • did you do the script on load or it's being called from somewhere? Commented Jul 2, 2014 at 2:54
  • I'm confused here. Why not move the height: CSS restriction and instead use something like min-height:, then the div will grow naturally to the content within it, width restrictions given of course. Commented Jul 2, 2014 at 2:59

2 Answers 2

1

you can use .each() function to get a callback for each article and use .children() to find only the element inside the current article and use it like this

$('article').each(function() {
    var $article = $(this);
    var highestCol = Math.max($article.children('.entry-sidebar').height(),$article.children('.inner-content-container').height());
    $article.children('.equalHeight').height(highestCol);
});

here's the working example in FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

1

It is because using height() as a getter method on a set of elements will return the height of the first element in the list.

So try

jQuery('article').height(function (i, height) {
    return Math.max(jQuery(this).find('.entry-sidebar').height(), jQuery(this).find('.inner-content-container').height());
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.