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

249
Visualizações
Can I add event to the button on click of input type=search?

Is there any event available for clicking clear button only appears on html5 search input element?

Here is my attempt. But if i use "search", the event is also triggered by pressing enter.

i.e. if i press enter, console prints out search clear

  mySearchBar.addEventListener("keydown", (e) => {
    if (e.keyCode === ENTER_KEY_CODE) {
       console.log("search");
       //Display search results
    }
  });

  mySearchBar.addEventListener("search", () => {
    console.log("clear");
    // Click the cross button of input type=search to Clear the search results and display all
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use eventListener 'input' to check if input is empty each time something append to input, then if no value that mean the search input was cleared.

EDIT: Added value testing in keydown event

mySearchBar = document.getElementById('search');
mySearchBar.addEventListener("keydown", (e) => {
  if (e.key === 'Enter') {
    if (!e.currentTarget.value)
      console.log('cleared');
    else
      console.log("search");
  }
});
mySearchBar.addEventListener('input', (e) => {
  if (!e.currentTarget.value)
    console.log('cleared');
})
<input type=search id=search>

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