Reputation: 15
I'm currently trying to make a script for educational purposes. Now I want to check the answers people give by using a little script. But I think everything is well-programmed, however it won't submit. Any ideas?
<div>
Ik<input id="invulvraag1" type="text" placeholder="h..."> Marie.
<button onclick="myFunction()">Controleer</button>
<p id="demo"></p>
</div>
<script>
function myFunction() {
var text = document.getElementById("invulvraag1").value;
var text;
// Het antwoord is correct
if (text === "heet") {
text = "perfect, goedzo!";
// het antwoord is iets anders
} else {
text = "helaas.. het had "heet" moeten zijn.";
}
document.getElementById("demo").innerHTML = text;
}
</script>
Upvotes: 0
Views: 70
Reputation: 310
If you want to post the form data on the server by using <form>
action
method then create button as
<input type="submit" onclick="return myFunction();">
<script>
function myFunction() {
var text = document.getElementById("invulvraag1").value;
var text;
// Het antwoord is correct
if (text === "heet") {
text = "perfect, goedzo!";
// het antwoord is iets anders
} else {
text = "helaas.. het had "heet" moeten zijn.";
}
document.getElementById("demo").innerHTML = text;
return true;
}
</script>
Or if you just want to call the client side function then run the code and click f12 in browser if there will be any error then it will throw in console
Upvotes: 0
Reputation: 4248
For frontend programming you will need to use console (dev tools). Press F12 or right click inspect element for safari.
In console tab you will see : js error :
SyntaxError: Unexpected identifier 'heet'
Fix : This line :
text = "helaas.. het had "heet" moeten zijn.";
must be :
text = "helaas.. het had 'heet' moeten zijn.";
OR
text = 'helaas.. het had "heet" moeten zijn.';
Upvotes: 1
Reputation: 31
<div>
<input id="invulvraag1" type="text" placeholder="h...">
<button onclick="myFunction()">Controller</button>
<p id="demo"></p>
</div>
<script>
function myFunction() {
var text = document.getElementById("invulvraag1").value;
var text;
// Het antwoord is correct
if (text === "heet") {
text = "perfect, goedzo!";
document.getElementById("demo").innerHTML = text;
// het antwoord is iets anders
} else {
text = "helaas.. het had "heet" moeten zijn.";
document.getElementById("demo").innerHTML = text;
}
}
</script>
use above code it is useful
Upvotes: 0