I'm very new to javascript so I apologize if this is a dumb question. I'm trying to write a function that will append a certain image next to an element depending on if it is true or false. I'm getting an error when I say src.appendChild(CorrectImg)
, it says "Cannot read property appendChild of null..."
function check(bool, id) {
var CorrectImg = document.createElement("CorrectImg");
CorrectImg.src = "correct.png"
var WrongImg = document.createElement("WrongImg");
WrongImg.src = "wrong.png";
if (bool) {
var src = document.getElementById(id);
src.appendChild(CorrectImg);
return;
}
var src = document.getElementById(id);
src.appendChild(WrongImg);
}