I'm currently working on a platform game and I've implemented a window.onload
event that is supposed to trigger.
Within this event, I am creating a div element, assigning it an ID, and then setting its onclick
property. Despite being confident in my syntax, the alert doesn't pop up when I click on the Mario div.
window.onload = showChoice;
// Function to display character options
function showChoice() {
var option1 = document.createElement("div");
option1.setAttribute("id","opt1_mario");
option1.onclick = function() {
alert("You chose Mario!");
}
document.getElementById("container").appendChild(option1);
var option2 = document.createElement("div");
option2.setAttribute("id","opt2_luigi");
option2.onclick = function() {
alert("You chose Luigi!");
}
document.getElementById("container").appendChild(option2);
}
The main gameplay works fine, but the issue lies with the click event not firing properly. Hopefully, someone can provide guidance on how to resolve this hiccup.