+ 1

How can I stop this function with the result?

I have code: <html> <h1>JavaScript Test</h1> <form name="quiz" id="quiz"> <div class="question">1. Jaki ciąg znaków jest używany do definiowania zmiennych?</div> <input id="answer" type="radio" name="group1" value="wrong">int<br> <input id="answer" type="radio" name="group1" value="correct">var<br> <input id="answer" type="radio" name="group1" value="wrong">vrb<br> <input id="answer" type="radio" name="group1" value="wrong">variable<hr> <div class="question">2. Jak jest nazywany "=" (znak równości) w JS?</div> <input id="answer" type="radio" name="group2" value="wrong">jest równoważne<br> <input id="answer" type="radio" name="group2" value="wrong">równy<br> <input id="answer" type="radio" name="group2" value="correct">jest to operator przypisania<hr> <script> var result = document.querySelector('#result'); var button = document.querySelector('button'); button.addEventListener('click', function() { var correct = 0; for(var i = 1; i <= 4; i++) { var radios = document.getElementsByName('group' + i); for (var j = 0; j < radios.length; j++) { var radio = radios[j]; if(radio.value == "correct" && radio.checked) { correct ++; } } } result.innerHTML = "Poprawne odpowiedzi: " + correct; break; }, false); The 'result' are moments displayed and disappears :/ How can I stop this function with result???

5th Jun 2017, 5:18 PM
Robert Koronny
Robert Koronny - avatar
4 odpowiedzi
+ 20
<html> <h1>JavaScript Test</h1> <form name="quiz" id="quiz"> <div class="question">1. Jaki ciąg znaków jest używany do definiowania zmiennych?</div> <input id="answer" type="radio" name="group1" value="wrong">int<br> <input id="answer" type="radio" name="group1" value="correct">var<br> <input id="answer" type="radio" name="group1" value="wrong">vrb<br> <input id="answer" type="radio" name="group1" value="wrong">variable<hr> <div class="question">2. Jak jest nazywany "=" (znak równości) w JS?</div> <input id="answer" type="radio" name="group2" value="wrong">jest równoważne<br> <input id="answer" type="radio" name="group2" value="wrong">równy<br> <input id="answer" type="radio" name="group2" value="correct">jest to operator przypisania<hr> <button id=res>Lol</button> <script> var res = document.querySelector('#res'); var butt = document.querySelectorAll('button')[0]; butt.addEventListener('click', function() { var correct = 0; for(var i = 1; i <= 4; i++) { var radios = document.getElementsByName('group' + i); for (var j = 0; j < radios.length; j++) { var radio = radios[j]; if(radio.value == "correct" && radio.checked) { correct ++; } } } res.innerHTML = "Poprawne odpowiedzi: " + correct; }, false); </script>
5th Jun 2017, 5:57 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
return instead of break ?
5th Jun 2017, 5:21 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
bummer
5th Jun 2017, 5:55 PM
Bloky246
Bloky246 - avatar
+ 1
when i use 'return correct'; and write result.innerHTML = correct; behind function i get result undefined :(
5th Jun 2017, 5:28 PM
Robert Koronny
Robert Koronny - avatar