Focus input field inside keydown event handler

July 2, 2019

If you focus an input field inside a keydown event handler, the value will actually end up inside the focused input field. It makes sense, I just never thought about it.


window.addEventListener('keypress', (event) => { document.querySelector('input').focus(); });

You can try it here:

Press any key while the input field is not focused.
The value will still end up inside the input field because it's being focussed in the keydown event handler.