'How to give a point when answer is correct with javascript
I have a (small) problem, I tried looking on the internet, but can't find the answer. I'm new with javascript.
HTML:
<form name="examen">
<input type="radio" id="1a" name="1" value="">Answer 1<br></input>
<input type="radio" id="1b" name="1" value="">Answer 2<br></input>
<input type="radio" id="1c" name="1" value="">Answer 3<br></input>
<input type="radio" id="1d" name="1" value="">Answer 4<br></input>
<input type="button" name="knop" value="Bereken" onClick="calculatie()">
</form>
JAVASCRIPT:
function calculatie() {
var calc = "Aantal vragen goed: ";
var totaal = parseInt(0);
if(document.examen.1c.checked){totaal += parseInt(1);}
calc += Math.round(totaal);
alert(calc);
}
Now, when a user is clicking on answer 3, the total points need to be one. But it is saying NaN, I can't get it right. The code needs to be in JAVASCRIPT/HTML. I can't use PHP.
Solution 1:[1]
try this function ...
function calculatie() {
var calc = "Aantal vragen goed: ";
var totaal = 0;
if(document.getElementById('1c').checked == true){
totaal += 1;}
calc += Math.round(totaal);
console.log(calc); //see console log for output or use it as u want
}
HERE is the fiddle
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sarath |
