I am using an array that has some objects in JavaScript, which is then displayed onto an HTML element, and this works. I would like to know how I can change the class if the team WON a match and changes the class if the team LOST a match.
I am using classList.add within an If/Else statement, but this does not seem to work, as it makes the colour of all the objects Red.
Below is my HTML code and JavaScript code.
HTML:
var guide = [{
result: "won",
match: "England 2-1 Belgium",
},
{
result: "lost",
match: "England 0-1 Denmark",
},
{
result: "won",
match: "England 3-0 Republic of Ireland",
},
{
result: "lost",
match: "Belgium 2-0 England",
},
{
result: "won",
match: "England 4-0 Iceland",
}
]
var text = "";
for (var i = 0; i < guide.length; i++) {
text += guide[i].match + "<br>";
if (guide[i].result == "won") {
document.getElementById("scores").classList.add('colourGreen');
} else if (guide[i].result == "lost") {
document.getElementById("scores").classList.add('colourRed');
}
}
document.getElementById("scores").innerHTML = text;
<div class="form-guide">
<div class="england-form-guide" id="englandFormGuide">
<p class="scores" id="scores"></p>
</div>
</div>
Many thanks.