I've added the whole loop. This is further included in another loop. var divid=1;
is defined before the loop starts and is incremented on each iteration. The console.log in the if else condition works. $('[id^="d"]').css("background-color","#43A942");
changes the css of ALL the elements in the whole page (!) whose ids starts with a 'd' but I want to make the changes only to elements in the loop. Is there any other way?
var 'util' equals 95 in this case
$(uname.weeklyData).each(function(i1,weekno){
var util=weekno.TotalUtilization;
trHTML += '<td id="'+tnum +'"><div style="background-color: rgb(255, 255, 255);"><div id="d'+divid +'" style="height: 20px; color: rgb(0, 0, 0); font-weight: bold;padding-left: 4px;width:'+util +'%;">' + weekno.TotalUtilization +'</div></div></td>';//displays TotalUtilization
if(util<=50){
$('[id^="d"]').css("background-color","#E9691E"); //orange
}
else if(util>50 && util<=80){
$('[id^="d"]').css("background-color","#049FD9"); //blue
}
else if(util>80 && util<=100){
$('[id^="d"]').css("background-color","#43A942"); //green
console.log("check");
}
else if(util>100){
$('[id^="d"]').css("background-color","#EBEBEC"); //gray
}
tnum=tnum+1;
divid=divid+1;
});
I am trying to append HTML rows to a table dynamically. I've given the ids for the div as d1,d2,d3...etc. How do I access that id to change that particular div's background color using JQuery?
When I explicitly type
$('#d185').css("background-color","#43A942");
The background color changes to green. But how to do this using the variable divid
in JQuery?I am new to coding and am unsure about the syntax, all the help is appreciated.