dick doeven dick doeven - 7 days ago 6
Javascript Question

button wont submit script

I'm currently trying t 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 wont 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>

Answer

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.';
Comments