Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda