Skip to content
Sign upLog in
← Back to Community
HTML
Profile icon
CandySuperCool

On submission, they should enter 9, then click submit. How do I make it so that if they enter 9 and click submit, it alerts "Correct!", but if they enter something else, it alerts "Wrong"?

Answered by Rak2006 [earned 5 cycles]
View Answer
Voters
Profile icon
CandySuperCool
Comments
hotnewtop
Profile icon
Rak2006

This should work.

In your js file:

document.getElementById("submit").addEventListener("click", function(e) { // On submit e.preventDefault(); var submission = document.getElementById("submission").value; // Submission value if (submission == 9) { //Check if equals 9 alert("Correct"); } else { alert("Wrong"); } });

In your HTML file, close the form and put the script tag underneath:

<form> <input id="submission" placeholder="What is 3 + 6"> <input id="submit" type="submit"> </form> <script src="script.js"></script>
Profile icon
Opromoto

8

Profile icon
Coder100

Move it outside of the form, the form has default behaviour where your page gets reloaded.