addEventListener keyup Keydown with Keycode not working

Der Admin81 picture Der Admin81 · Dec 2, 2014 · Viewed 39.7k times · Source
<input class="inputVal">
<script type="text/javascript">

    var myVal = document.getElementsByClassName('inputVal')[0]; 

    myVal.addEventListener('keyup', function(e){
        var count = myVal.value;
        console.log(count)
    });  
</script>

Answer

Leo picture Leo · Dec 2, 2014

Try bind event to document or window. If an element is not focusable and is not focused, keyboard events won't dispatch to it, instead they dispatch to document root(<body>).