Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

text disappears rapidly after click on 'submit-button'

Tags:

javascript

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?

like image 708
user114158 Avatar asked Jul 01 '12 20:07

user114158


4 Answers

The submit buttons are submitting your form (so reloading your page).

Change the buttons to type="button".

like image 78
Philippe Leybaert Avatar answered Nov 11 '22 18:11

Philippe Leybaert


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.

like image 40
Ry- Avatar answered Nov 11 '22 17:11

Ry-


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" />
like image 2
Praveen Kumar Purushothaman Avatar answered Nov 11 '22 17:11

Praveen Kumar Purushothaman


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

like image 1
Siva Charan Avatar answered Nov 11 '22 17:11

Siva Charan