D. Giankakis D. Giankakis - 4 months ago 8
jQuery Question

Jquery stops before for loop

i am trying to adjust a div font-size depending on its height

i get the elements with class "prod-name" with .each



$prod_names = new Array();
$i=0;
$j=0;

$(".prod-name").each(function(){
$prod_names[$i] = $(this);
$i++;
});

for($j=0;$j>=$i;$j++){
console.log("in for");
if(($prod_names[$j].height() > 20) && ($prod_names[$j].height() <= 40)){
console.log("ok");
$prod_names[$j].css("font-size","0.9em");
}else if($prod_names[$j].height() > 40){
$prod_names[$j].css("font-size","0.8em");
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="mostviewed" class="products">
<h2 class="cat-title">MOST VIEWED</h2>
<ul>
<li class="scol25 mcol25 lcol25" >
<a href="product.php" class="prod-img"><img src="images/products/.jpg" title=""/></a>
<p class="prod-name">Intel Core I3</p>
<div class="prices">
<p id="original-price" class="scol50 mcol50 lcol50 price">&euro;</p>
<p id="low-price" class="scol50 mcol50 lcol50 price">&euro;</p>
</div>
<div class="fa-buttons">
<a id="morea" class="scol50 mcol50 lcol50 add1" title="More information" href="product.php"><i class="fa fa-search" aria-hidden="true"></i></a>
<a id="fava" href="#" class="scol50 mcol50 lcol50 add1" title="Add to favorites"><i class="fa fa-heart" aria-hidden="true" ></i></a>
</div>
</li>
</ul>
</div>





i added logs to see if it reaches the code in the loop and it doesnt

note: in reality i use php but for better understanding of my problem i changed it to html

Thank you

KAD KAD
Answer

You have a problem with the logic here... Assuming there are 4 prod-name elements, then $i will be 4, while $j is 0, so $j>=$i is false hence won't get in the loop..

I bet you shall reconsider the aim of this code and write it in a neater way.