Got a problem. this is my code:
<html>
<body>
<form>
<p>13-7: <input id="in1" type="text"" /><input type="submit" onclick="check(6, 'in1', 'out1')" value="Tjek!"/></p>
<p id="out1"></p>
<p>20-7: <input id="in2" type="text"" /><input type="submit" onclick="check(13, 'in2', 'out2')" value="Tjek!" /></p>
<p id="out2"></p>
<script type="text/javascript">
function check(facit, input, output) {
var answer, evaluation;
answer = document.getElementById(input).value;
evaluation = (answer == facit) ? "Correct" : "Wrong";
document.getElementById(output).innerHTML = evaluation;
return false;
}
</script>
</form>
</body>
</html>
When I click the submit-button, the 'correct/wrong' shows only for a moment. I want it to stay on the site, any advice?
The submit buttons are submitting your form (so reloading your page).
Change the buttons to type="button"
.
Your buttons are submit buttons (<input type="submit">
) so they will submit the form and refresh the page on each click. Change your buttons to <input type="button">
instead.
Change the onclick function from:
onclick="check(13, 'in2', 'out2')"
to:
onclick="return check(13, 'in2', 'out2')"
Alternatively, if you don't want the form to be submitted, as you have two Submit buttons, it is better to use:
<input type="button" />
Just change onclick from this
onclick="check(6, 'in1', 'out1')"
to
onclick="check(6, 'in1', 'out1'); return false;"
Do the same way for the other one too.
Remove return false;
on the check
function.
Refer LIVE DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With