<input type="text" id= 'input'>
let input = document.getElementById('input');
//add event listener to keydown event
input.addEventListener('keydown',(e)=> {
console.log(e.key);
//check the key is 'Escape'
if( e.key === 'Escape') {
input.select();}
});
when I press 'Esc', it didn't work it only blur, don't select what I type in the input element. Where did I make mistake?
for pure js
document.onkeydown = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 27) {
alert('Esc key pressed.');
}
};
for jquery
jQuery(document).on('keyup',function(evt) {
if (evt.keyCode == 27) {
alert('Esc key pressed.');
}
});
Your code is running fine. Check it on JS Bin.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.