This if statement is meant to change text to one of the colors with a button for each color. However, every button only changes the text to red as is. I'm not sure what i am doing incorrectly.
Javascript:
function colorFunction() {
if (document.getElementById("red")) {
document.getElementById('test').style.color = "red";
}else if(document.getElementById("blue")) {
document.getElementById('test').style.color = "blue";
}else if (document.getElementById("black")) {
document.getElementById('test').style.color = "black";
}
}
Html:
<button id="red" style="background-color:red" type="button" onclick="colorFunction()"><font color="white">Red Text</font></button>
<button id="blue" style="background-color:blue" type="button" onclick="colorFunction()"><font color="white">Blue Text</font></button>
<button id="black" style="background-color:black" type="button" onclick="colorFunction()"><font color="white">Black Text</font></button>
document.getElementById("red")returns a dom element which is a truthy value so always the firstifwill be executed if theredelement is present rest of theelse...ifwill be ignored