What I am trying to do is write the conditional for the boolean value of my buttons. I have 4 buttons, 3 false and one true. I want a block of code to execute for the false values, and another to execute for the true value.
This is what I have so far.
function startGame() {
quiz.innerHTML = "<h1>Choose the tag that corresponds with <br> the definition of an empty tag</h1>";
startBtn.innerHTML = "";
var button = document.createElement("button");
button.textContent = "The p tag";
questionBtn.appendChild(button);
button.setAttribute("style", "background-color: black; color: white; font-size: 20px; width: 100px;")
var button2 = document.createElement("button");
button2.textContent = "The nav tag";
questionBtn.appendChild(button2);
button2.setAttribute("style", "background-color: black; color: white; font-size: 20px; width: 100px;")
var button3 = document.createElement("button");
button3.textContent = "The img tag";
questionBtn.appendChild(button3);
button3.setAttribute("style", "background-color: black; color: white; font-size: 20px; width: 100px;")
var button4 = document.createElement("button");
button4.textContent = "The h1 tag";
questionBtn.appendChild(button4);
button4.setAttribute("style", "background-color: black; color: white; font-size: 20px; width: 100px;")
var btns = document.querySelectorAll("button")
for (i of btns) {
i.addEventListener("click", function() {
var button = false;
var button2 = false;
var button3 = true;
var button4 = false;
if (i === true) {
prompt("hello")
} else {
alert("hello")
}
});
}
I’m not quite sure how to target the the button3 with the true value. Currently, every button is returning the alert.
2
Answers
You could use an index to target the special element.
If you delegate and use an object array, your quiz will be much simpler