I need to execute a button click event upon pressing key enter.
As it is at the moment the event is not firing.
Please Help me with the syntax if possible.
$(document).on("click", "input[name='butAssignProd']", function () {
//all the action
});
this is my attempt to fire click event on enter.
$("#txtSearchProdAssign").keydown(function (e) {
if (e.keyCode == 13) {
$('input[name = butAssignProd]').click();
}
});
try out this....
Find Demo in jsbin.com
Are you trying to mimic a click on a button when the enter key is pressed? If so you may need to use the
trigger
syntax.Try changing
to
If this isn't the problem then try taking a second look at your key capture syntax by looking at the solutions in this post: jQuery Event Keypress: Which key was pressed?
Another addition to make:
If you're dynamically adding an input, for example using
append()
, you must use the jQueryon()
function.UPDATE:
An even more efficient way of doing this would be to use a switch statement. You may find it cleaner, too.
Markup:
jQuery:
This appear to be default behaviour now, so it's enough to do:
You can try it in this JSFiddle
Tested on: Chrome 56.0 and Firefox (Dev Edition) 54.0a2, both with jQuery 2.2.x and 3.x
You were almost there. Here is what you can try though.
I have used
trigger()
to execute click and bind it on thekeyup
event insted ofkeydown
becauseclick
event comprises of two events actually i.e.mousedown
thenmouseup
. So to resemble things same as possible withkeydown
andkeyup
.Here is a Demo
Just include preventDefault() function in the code,