I am a beginner in JavaScript and I am not allowed to use jQuery or HTML in my assignment. I am trying to change the color of different items in my JSON when clicked, but it only works for the first item in the list. When I try to apply it to the second item by switching around IDs, it doesn't work.
for(var i = 0; i < content.length; i++){
var contentText = content[i];
var textCH1_2 = document.createElement("p");
textCH1_2.id = "peterEntersRoom";
if(content[i].id === "peterEntersRoom"){
textCH1_2.innerText = contentText.text;
}
textContainer1.append(textCH1_2);
}
document.getElementById("peterEntersRoom").onclick = function() {
document.getElementById("peterEntersRoom").style.color = "green";
}
The structure of the JSON is as follows:
{
"chapter": 1,
"id": "peterEntersRoom",
"character": {
"main_character": "Mrs. Darling",
"supporting_character" : [
"Wendy"
],
},
"dialogue" : false,
"text": "Of course"
},
Can someone please assist me?