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

116
Visualizações
How to trigger search on enter key and button click javascript?

I want to trigger the search button to work when the "search" button is clicked and when the enter key on the keyboard is pushed. So far, the search works when the button is clicked. How do I add that to this code?

Thanks!

//Search button click
document.getElementById("search").addEventListener("click", () => {
    //initializations 
    let searchInput = document.getElementById("search-input").value;
    let elements = document.querySelectorAll(".question-name");
    let cards = document.querySelectorAll(".card");



    //loop through all elements 
    elements.forEach((element,index) =>{
        //check if text includes  the search value
        if(element.innerText.includes(searchInput.toString())){
            //display matching card
            cards[index].classList.remove("hide");
        }
        else {
            //hide others 
            cards[index].classList.add("hide");
        }

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

0

document.getElementById('search').addEventListener('keyup',function(e) {
 if(e.code==13) {/*your code goes here*/}
  })

More informations: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code

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