Is it possible to execute a function by pressing the enter key on the keyboard only if a certain textbox has focus. i.e. if any other textbox has focus or no textbox has focus, the enter key should do nothing.
You can execute a function by pressing the enter key in a field using the key event in JavaScript. If the user presses the button use the keydown to get know its enter button or not. If it enters the key then call the JavaScript function.
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.
Solution 1function (event) { if (event. which == 13 || event. keyCode == 13) { //code to execute here return false; } return true; }); Implement it according to your need.
$('#myTextbox').bind('keyup', function(e) {
if ( e.keyCode === 13 ) { // 13 is enter key
// Execute code here.
}
});
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