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

177
Vistas
Key press shows the key character

I have a search box which is by default not autofocused. So i am putting a keypress event on the body i.e. when forward slash (/) is pressed the search box should be in focus. Although on key press it puts the search box in focus but also puts the "/" in the search box.

body.onkeypress = (e) => {
    if(e.which === 47) {
        searchInput.focus()
    }
} 

How should I fix this?

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

0

Use preventdefault to prevent the '/' from being typed.

Make sure you do this in your if statement, otherwise all characters get blocked.

body.onkeypress = (e) => {
    if(e.which === 47) {
        e.preventDefault()
        searchInput.focus()
    }
} 

https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

about 4 years ago · Juan Pablo Isaza Denunciar

0

  <body id="body">
  <input type="text" id="elementid" />
</body>
<script>
  document.onkeypress = (e) => {
    if (e.which === 47) {
      document.getElementById("elementid").focus();
      setTimeout(() => {
        str = document.getElementById("elementid").value = document
          .getElementById("elementid")
          .value.slice(0, -1);
      });
    }
  };
</script>
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