Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Check the answer

I try to see if the word entered in the form is the correct one. If it is correct then I open another page and otherwise I will get an error message, but I don't know how to make the script for this. Also I don't want to use a submit button.

<form id="form">
    <input type="text" name="inputBox" placeholder="Enter your answer"><br> 
</form>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const input = document.querySelector('input');
const error = document.querySelector('p.error');
const woohoo = document.querySelector('p.woohoo');

const correctAnswer = 'foo bar baz';

const handleInput = (condition) => {
    if (condition) {
        error.style.display = 'block';
        woohoo.style.display = 'none';
        return;
    }

    error.style.display = 'none';
    woohoo.style.display = 'block';

    window.open('https://google.com');
};

input.addEventListener('keyup', () => handleInput(input.value !== correctAnswer));
<form id="form">
    <input type="text" name="inputBox" placeholder="Enter your answer" />
    <p class="error" style="color: red; display: none">Incorrect</p>
    <p class="woohoo" style="color: green; display: none">Correct</p>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

Try this: In this code I check with the key event, if I press enter I call and ask if the answer is "Hello" is correct and I open another page, otherwise I send an alert with an error

<form id="form">
    <input id="MyEnter" type="text" name="inputBox" placeholder="Enter your answer"><br> 
</form>

<script>
var myenter = document.getElementById("MyEnter");
myenter.addEventListener("keypress", function(event) {
  if (event.key === "Enter") {
    event.preventDefault();
    var answer = 'Hello'
    var mytext = document.getElementById("MyEnter").value;
    if (mytext==answer) {
        alert('opening another page');
        window.open("https://www.google.com");
    }else{
        alert("Incorrect answer");
    }
  }
});
</script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!