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

81
Vistas
Document keydown event propagates to a input child with stopPropagation

I have a problem with document event keydown. When i pressed on specifics keys (1 or 2), the event keydown propagates to input child. I used event.stopPropagate() but i doesn't work.

Here my code :

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>TEST KEYDOWN EVENT</title>
</head>
<body>
    <form>
        <label>Number of Series</label>
        <input type="number" name="nbSeries" value="10" id="nbSeries"/>
    </form>

    <script type="text/javascript">

        document.addEventListener('keydown', keyPressed);

        function keyPressed(e){

            e.stopPropagation();

            if(e.code == "Digit1" || e.code == "Digit2"){
                console.log('You pressed ' + e.code);
            }
        }
    </script>
</body>
</html>

On console, when i change my input (#nbSeries) value using key 1 and 2, display : You pressed "keyCode"...

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

0

stopPropagation only stops the event from propagating - stopping other listeners later from seeing the event. That does not stop the default behavior of the action. For example, it doesn't stop characters from appearing when typing into an input field. To achieve that, you need preventDefault instead.

document.addEventListener('keydown', keyPressed);
function keyPressed(e) {
  e.preventDefault();
  if (e.code == "Digit1" || e.code == "Digit2") {
    console.log('You pressed ' + e.code);
  }
}
<form>
  <label>Number of Series</label>
  <input type="number" name="nbSeries" value="10" id="nbSeries" />
</form>


Also, although it's not directly related to the problem you were experiencing, there's also something to keep in mind about how event propagation works. Listeners trigger in this order:

  • The topmost element (the window), capturing phase
  • The document, capturing phase
  • Ancestors of the target, capturing phase, descending towards the target, capturing phase
  • The target
  • Ancestors of the target, bubbling phase
  • ...

Since your document keydown listener is listening for bubbling events, it triggers only after the event's reached the target's listeners. If you were trying to have a listener on the document stop a listener on a child element from triggering, you would need to add the document listener in the capturing phase instead to prevent propagation downwards to the target, which can be done by passing true to addEventListener's third parameter.

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