Syden Syden - 26 days ago 6
jQuery Question

jQuery numeric array comparison

HTML:

<section class="DataContent"></section>


Content is appended dynamically on .DataContent through jQuery arrays loop

see JSFiddle

How could I make li.rate appear on red if span.ratePercent is < 0, on green if span.ratePercent > 0 and on grey if span.ratePercent == 0?

Trying some functions like:
calculateRate()
in the fiddle but it always end up affecting all li.rate color equally.

Thank you for your time!

Answer

You can use benefit of your loop for appending li elements. Just change your li element to:

<li class="rate'+[i]+'">

And in calculateRate() function change the .rate class for every if statement:

$(".rate"+[i])

Complete working code : jsFiddle

Now you can have as many as you want list objects, and it will loop throught each, and according from their ratePercent value it will set properly color of text.