jeudi 22 mars 2018

jQuery each: If element is in the element

Why if I have several divs, which I want to give different height depending on the content. He always takes the height from the first condition.

$('.box-site-promo').each(function(index) {

  console.log($(this));

  if ($(this).find('.box-site-tags')) {
    heightBox = 200;
    console.log(index + " " + heightBox);
  } else if ($(this).find('.box-site-authors')) {
    heightBox = 100;
    console.log(index + " " + heightBox);
  } else {
    heightBox = 300;
    console.log(index + " " + heightBox);
  }

  $(this).height(heightBox);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div class="box-site-promo"> A
  <div class="box-site-tags">some tags</div>
</div>
<div class="box-site-promo"> B
  <div class="box-site-authors">authors</div>
</div>
<div class="box-site-promo"> C
  <div class="box-site-tags">some tags</div>
</div>
<div class="box-site-promo"> D </div>

Aucun commentaire:

Enregistrer un commentaire