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

296
Visualizações
Vanilla Javascript: How do I create a working searchbar with hidden results?

I am pretty new to Javascript and have been using it for almost a month. I am trying to create a website easter egg that can be found by typing a hidden word into the searchbar. My problem mostly has to do with making the variable value read the strings in the array. There were no error messages that popped up.

Here is my code:

let searchResults = ['dog', 'cat', 'bird'];
let searchInput = document.querySelector('.search-box .input-box #searchbar');

function searchBar() {
  searchInput.addEventListener("keyup", function(event) {
    if (event.keyCode == 13) {
    } else {
    }
    });
  searchInput.addEventListener("input", function(event) {
    let value = event.target.value;
    if (value == searchResults[2] && (event.keyCode == 13)) {
      window.location.href = "bird.html";
    } else {
    }
  });
}

There is also a small additional issue with the keycode firing off multiple times. I'm not too worried about this, but it would be nice to find a solution for it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no "keyCode" property for an input event. The keyCode exists only on keyboard events, but input is it's own, separate type of event.

Use the keyup event instead. There's really no reason to have two separate event handlers anyway.

let searchResults = ['dog', 'cat', 'bird'];
let searchInput = document.querySelector('#searchbar');


searchInput.addEventListener("keyup", function(event) {
  if (event.keyCode == 13) {
    
    let value = event.target.value;
    if(value == searchResults[2]){
    
      // window.location.href = "bird.html";
      console.log("birb");
    
    }else{
    
      // User hit enter but it wasn't bird...
    }
    
  }
});
<input id=searchbar>

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