Search code examples
javascriptkeycode

JavaScript: What should keyCode be replaced with?


this is the js code:

document.addEventListener('keyup', function (e) {
    e.preventDefault();
    if (e.keyCode === 191) {
        document.getElementById('search-input').focus()
    }
})

but in visual studio code says: keyCode is deprecated. What should this code be replaced with? Is there any need to do this at all?


Solution

  • Replace it with e.key === 'Enter' for example or e.code === 'Enter'. You can find more information about it here and here.