I need the user to type a word and if its the right keyword, I need it to change pages.
my code
<form action="/yay.html"> <label>what's 1+1</label><br> <input type="text"><br> <input type="submit" value="Submit"> </form>
for example I want something a bit like this.
<script> function checkstuff() { if text = "hello world" } </script> //so a function checks if the text is equal to "hello world" <form action="/yay.html"> <input type="text"> <input type="submit" value="Submit" onclick="checkstuff()"> //submit input runs function </form>
Advertisement
Answer
In the solution below, the user input is evaluated to enable/disable the disabled attribute of the submit button.
const submit = document.getElementById('submit'); let answer = document.getElementById('answer'); const target = 2; function log(value, mode) { console.clear(); if(mode) console.log(`Information: ${value}`); else console.log(`Input: ${value}`); } answer.addEventListener('input', function(event) { log(this.value, true); if(parseInt(this.value) == target) { submit.disabled = false; } else { submit.disabled = true; } }); submit.addEventListener('click', function(event) { log("Button fired.", false); });
<form action="/yay.html"> <label>what's 1+1</label><br> <input id="answer" type="text"><br> <input id="submit" type="submit" value="Submit" disabled> </form>