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

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

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 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