javascript - Run through array and check values of <td> cells in jQuery -
scenario: have table 10 rows, each containing of class '.t3_dc'.
these cells contain value of between 1.0 , 9.9
for each td, want check if score/value between amount , if so, change colour. example, < 7.0 red, 7-8 stay white, above 8 green.
i got far after few attempts biggest obstacle seems storing each array , running through , checking them individually.
$(document).ready( function() { function scores () { var score = $('td.t3_dc').text(); var num = parsefloat(score); alert(score)[0]; if(num < 7 ) { $(score).css('color','green'); } else { $(score).css('color','red'); } }; settimeout(scores, 2000); });
note: timeout function exists table takes couple of seconds load on page
q. how can loop through array of 's , check each value, adding colour necessary?
imho, don't need timeout, function first fire when page loaded. loop classes using each()
, check it's value.
$(function() { $('.t3_dc').each( function() { var elem = $(this) , value = parsefloat( elem.text() ); if( value < 7 ) { elem.css('color', 'red'); } elseif( value > 8 ) { elem.css('color', 'green'); } }); });
Comments
Post a Comment