My goal is to determine if a group of strings contain a specific word, and based on that, change the background color. If any member within the group has the word 'unlikely', I want the background color to be green; otherwise, it should be red. The desired outcome is to have some members with green backgrounds and others with red backgrounds.
Unfortunately, my code is not functioning as expected.
This is what I've attempted:
function colorCoding() {
var str = document.getElementsByClassName("colorString").innerHTML;
var n = str.includes("unlikely");
if (n == true) {
document.getElementById("colorString").style.backgroundColor = "green";
} else {
document.getElementById("colorString").style.backgroundColor = "red";
}
}
However, the background color remains unchanged despite running the above code.
I did manage to get it to work for individual IDs, but doing so for each member of the class would be time-consuming. Is there a way to achieve this using the class or perhaps by modifying the function to apply to the entire set?
Any assistance you can provide would be greatly appreciated!