javascript - Make table cell value red if value is >= 0 -
how can make table cell value if it's >= 0 makes table rows background color red?. , way value fetched database.
using vanilla js, after edit: (here working fiddle) (in 1 entire row gets colored red)
window.onload = function(){ // after contents has loaded var cells=document.getelementsbytagname("td"); //select table cell tags for(var i=0;i<cells.length;i++){ //iterate through each of them //check if content more 0 if(parsefloat(cells[i].textcontent || cells[i].innertext)>=0){ cells[i].style.backgroundcolor="red"; //change background red } } };
if need support modern browsers, think this solution prettier:
window.addeventlistener("domcontentloaded", function(e) { document.getelementsbytagname("td").filter(function(elem){ return parsefloat(elem.textcontent) >= 0; }).foreach(function(elem){ elem.style.backgroundcolor="red"; }); }, false);
old content, jquery solution:
$(function(){ //after dom loaded $("td").each(function() { if(parsefloat($(this).text()) >= 0){ //for every element text's float value less 0 $(this).css("background-color","red"); //change background color red } } }
Comments
Post a Comment