To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.
The “enter” key is represent by code “13”, check this ASCII charts. To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox').
To check whether user pressed ENTER key on webpage or on any input element, you can bind keypress or keydown event to that element or document object itself. Then in bind() function check the keycode of pressed key whether it's value is 13 is not.
The keypress() method triggers the keypress event, or attaches a function to run when a keypress event occurs. The keypress event is similar to the keydown event. The event occurs when a button is pressed down. However, the keypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC).
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>
Find Demo in jsbin.com
Try This
$('#twitterSearch').keydown(function(event){
var keyCode = (event.keyCode ? event.keyCode : event.which);
if (keyCode == 13) {
$('#startSearch').trigger('click');
}
});
Hope it helps you
$('#txtSearchProdAssign').keypress(function (e) {
if (e.which == 13) {
$('input[name = butAssignProd]').click();
return false;
}
});
I also just found Submitting a form on 'Enter' which covers most of the issues comprehensively.
You were almost there. Here is what you can try though.
$(function(){
$("#txtSearchProdAssign").keyup(function (e) {
if (e.which == 13) {
$('input[name="butAssignProd"]').trigger('click');
}
});
});
I have used trigger()
to execute click and bind it on the keyup
event insted of keydown
because click
event comprises of two events actually i.e. mousedown
then mouseup
. So to resemble things same as possible with keydown
and keyup
.
Here is a Demo
Another addition to make:
If you're dynamically adding an input, for example using append()
, you must use the jQuery on()
function.
$('#parent').on('keydown', '#input', function (e) {
var key = e.which;
if(key == 13) {
alert("enter");
$('#button').click();
return false;
}
});
UPDATE:
An even more efficient way of doing this would be to use a switch statement. You may find it cleaner, too.
Markup:
<div class="my-form">
<input id="my-input" type="text">
</div>
jQuery:
$('.my-form').on('keydown', '#my-input', function (e) {
var key = e.which;
switch (key) {
case 13: // enter
alert('Enter key pressed.');
break;
default:
break;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With