The title is self-explanatory, I tried using .checked(and several other methods that also failed), but it did not work.
I want to know how, so I can count the score for a quiz.
Here is the html part for it:
<html>
<head lang=pt>
<meta charset="utf-8">
<title>Formulario</title>
<link rel="stylesheet" type="text/css" href="quiz1.css">
<script src=quiz.js></script>
</head>
<body>
<form class=formulario onsubmit="return mostrar_placar()">
<h3 id = "pergunta">Qual é o nome do inventor da linguagem de programação Python?<br></h3>
<input class = "escolhas" id ="0" type="button" value="Guido van Rossum" onclick="keep_highlighted('0')"><br>
<input class = "escolhas" id ="1" type="button" value="Dennis Ritchie" onclick="return keep_highlighted('1')"><br>
<input class = "escolhas" id ="2" type="button" value="James Gosling" onclick="return keep_highlighted('2')"><br>
<input class = "escolhas" id ="3" type="button" value="Brendan Eich" onclick="return keep_highlighted('3')"><br>
<h3 id = "pergunta">Dentre as alternativas a seguir, qual não é um item de hardware?<br></h3>
<input class = "escolhas" id ="4" type="button" value="Mouse" onclick="return keep_highlighted('4')"><br>
<input class = "escolhas" id ="5" type="button" value="Processador" onclick="return keep_highlighted('5')"><br>
<input class = "escolhas" id ="6" type="button" value="Chipset" onclick="return keep_highlighted('6')"><br>
<input class = "escolhas" id ="7" type="button" value="Debian" onclick="return keep_highlighted('7')"><br><br>
<input type="submit" value="confirmar">
</form>
</body>
</html>
And this is the js:
var certos = ["0", "7"];
function keep_highlighted(id) {
document.getElementById(id).style.background = "white";
document.getElementById(id).style.color = "black";
}
function placar() {
var placar = 0;
for (var i = 0; i < 8; i++) {
console.log(document.getElementById(i.toString()).checked);
if (document.getElementById(i.toString()).checked) {
if (document.getElementById(i.toString()).value == certos[i]) {
placar += 1;
}
}
}
return placar;
}
function mostrar_placar() {
alert("Seu placar é " + placar());
}
The console is only printing 8 falses no matter what I click(showing that its never checked). So it never enters in the condition that counts the score (without if (document.getElementById(i.toString()).checked) it always shows 2 on the score since it loops through all the buttons even the ones not selected). And with it always shows the score as 0....can someone help?