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
Detect Ie Version (Prior to V9) in JavaScript
Change Url Parameters and Specify Defaults Using JavaScript
Getting Value of Select (Dropdown) Before Change
How to Wait for Set of Asynchronous Callback Functions
Is Right Click a JavaScript Event
Promise.All Consumes All My Ram
How to Pass Data from a Page to Another Page Using React Router
How to Set the Value Property in Angularjs' Ng-Options
How to Check a Radio Button with Jquery
How to Get Element by Xpath Using JavaScript in Selenium Webdriver
How to Loop Through an Array Containing Objects and Access Their Properties
How to Pass JSON Post Data to Web API Method as an Object
Get String in Yyyymmdd Format from Js Date Object
What Is the JavaScript String Newline Character
Mongoose and Multiple Database in Single Node.Js Project