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

73
Vistas
performing a search on enter?

I have this form:

<form>
  <label for="locationsearch">Location:</label>
  <input type="search" id="locationsearch" name="locationsearch" />
</form>

I want to add an eventListener when I hit enter on the input(i.e. #locationsearch). I tried doing this:

const locationSearch = document.getElementById("locationsearch");
    locationSearch.addEventListener("search", () => {
      console.log("search entered");
    });

and this:

const locationSearch = document.getElementById("locationsearch");
locationSearch.onsubmit = function () {
  console.log("search entered");
};

Both are not console-logging.

What is the correct/better way to perform this action?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The onsubmit event would happen on the form itself, not the input. So you could use an id on the form instead to target it directly.

const locationSearch = document.getElementById("locationsearch");
locationSearch.onsubmit = function () {
  console.log("search entered");
};
<form id="locationsearch">
  <label for="locationsearch">Location:</label>
  <input type="search" name="locationsearch" />
</form>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could handle it keydown event handler of input element. And check the key code if Enter key pressed.

const locationSearch = document.getElementById("locationsearch");
locationSearch.addEventListener("keydown", (e) => {
  if (e.code === 'Enter') {
     // Do Something ? Search
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use keypress event for this.

const locationSearch = document.getElementById("locationsearch");
    locationSearch.addEventListener("keypress", () => {
      if (event.key === "Enter") {
    event.preventDefault();
    let inputVal = document.getElementById("locationsearch").value;
    console.log("search entered "+inputVal);
    document.getElementById("locationsearch").value = "";
  }
});
<form>
  <label for="locationsearch">Location:</label>
  <input type="search" id="locationsearch" name="locationsearch" />
</form>

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