Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda