This JavaScript code is to change the background of a div if the content is different:
function myfunction() {
$a = document.getElementById('t').getElementsByTagName('div');
var x = 0;
while(x < $a.length) {
var b = ($a[x].innerHTML);
b.toString;
alert(x + b[x]);
if(b == 'PENDING SELECTING TEAM MEMBER' ||b == 'PENDING APPROVAL BY SCHEME MANAGER' ||b == 'PENDING SELECTING TEAM MEMBER' ||b == 'PENDING REVIEW TEAM MEMBER BY LEAD ASSESSOR' ||b == 'PENDING ACKNOWLEDGEMENT BY TA/LA' ||b == 'PENDING PREPARE AUDIT PLAN BY LA' ||b == 'PENDING REVIEW AUDIT PLAN BY AO' ||b == 'PENDING ACCEPT LA BY LAB' ||b == 'PENDING VERIFY REASON BY AO' ||b == 'PENDING CONDUCT ASSESSMENT' || b == 'PENDING CORRECTIVE ACTION BY CAB' || b == 'PENDING REVIEW BY AO' || b == 'PENDING CORRECTIVE ACTION APPROVAL BY SCHEME MANAGER' ||b == 'PENDING APPROVAL BY DOA' ){
document.getElementById('status'+x).style.background="maroon";
alert(x + b);
}
x++;
}
}
It doesn’t work because the if statement is failing.
Can someone help me figure out what’s wrong?
Just as a note,
b.toString;should beb.toString();And personally I prefer using double quotes, but that’s just due to being used to strongly type them and having a difference between characters and Strings.
Saw others say it, but just to emphasize : a variable in a function not having the ‘var’ keyword makes them global, meaning they are accessible, and changeable in any scope in the program.
Try using the following javascript code : JSFiddle
Changed the way you set the background colour.