I need to close a modal pop-up using jQuery on Enter
key press, so i would like to know how can I know that Enter was pressed using jquery or javascript?
Quick help is needed/appreciated
thanks
You can use event.which
to check which keyboard key was pressed using jquery, so your code can be
$(".Selector").keypress(function(event) {
if(event.which == 13) { //13 is for enter
alert('Enter pressed');
}
});
you can find the complete keyboard keycode here
Using jQuery code to check which key was pressed
$(function () {
$(document).on('keyup keydown keypress', function (event) {
event.preventDefault(); //prevent from performaing default action
if (event.keyCode == 13) {
alert("Enter key pressed");
}
else {
alert("Not Enter key pressed");
}
});
});
Using javascript
document.onkeydown = checkKeyEvent;
document.onkeypress = checkKeyEvent
document.onkeyup = checkKeyEvent;
function checkKeyEvent(e) {
if (e.keyCode == 13) //Enter key code =13
{
alert('Enter pressed');
}
else //Other than Enter key pressed
{
alert('Not Enter key pressed');
}
}
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly