How to Detect Pressing Enter on the Keyboard Using Jquery

How can I detect pressing Enter on the keyboard using jQuery?

The whole point of jQuery is that you don't have to worry about browser differences. I am pretty sure you can safely go with enter being 13 in all browsers. So with that in mind, you can do this:

$(document).on('keypress',function(e) {
if(e.which == 13) {
alert('You pressed enter!');
}
});

Jquery: how to trigger click event on pressing enter key

try out this....

$('#txtSearchProdAssign').keypress(function (e) {
var key = e.which;
if(key == 13) // the enter key code
{
$('input[name = butAssignProd]').click();
return false;
}
});

$(function() {

$('input[name="butAssignProd"]').click(function() {
alert('Hello...!');
});

//press enter on text area..

$('#txtSearchProdAssign').keypress(function(e) {
var key = e.which;
if (key == 13) // the enter key code
{
$('input[name = butAssignProd]').click();
return false;
}
});

});
<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>

<body>
<textarea id="txtSearchProdAssign"></textarea>
<input type="text" name="butAssignProd" placeholder="click here">
</body>

</html>

How to detect if Enter Key was pressed within a DIV using jquery?

Try this

 <div id="Div1">
<input type ="text" id="aa"/>
sdfsd hsjdhsj shdj shd sj
</div>

Jquery

$(function(){
$("#Div1 input").keypress(function (e) {
if (e.keyCode == 13) {
alert('You pressed enter!');
}
});
});

Demo

jquery detect enter key on dynamically created textbox

Use .on for dynamic HTML - detect a keyup event and then detect if the key up was for the enter key:

$(".vocab-list").on("keyup", "#writeWord", function(e) {
if (e.which == 13) {
alert("Enter");
}
});

Detect Enter key is pressed with jQuery

<input type="text" id="txt">

$('#txt').keydown(function (e) {
if (e.keyCode == 13) {
alert('you pressed enter ^_^');
}
})

On jsfiddle.

Update (June 2022)

Note that keyCode has been deprecated. Use KeyboardEvent.code or KeyboardEvent.key properties instead:

if (e.key === "Enter") {
// Pressed enter
}

Keypress enter on button click in jQuery

try this.

var e = $.Event( "keypress", { which: 13 } );
$('#yourInput').trigger(e);

Capture an Enter Key Pressed anywhere on the page

$(document).keypress(function(e) {
if(e.which == 13) {
// enter pressed
}
});


Related Topics



Leave a reply



Submit