I am calling a function on button click code is given blow:
<input type="button" value="Search" id="go" />
$("#go").click(function ()
{
...
});
now I catch if user hit enter key from keyboard by this function:
$("#s").keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
}
});
but how could I call
$("#go").click(function ()
{
...
});
both if user hits enter key & on click GO button?
To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox'). keypress(function(event){ var keycode = (event.
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.
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.
Definition and Usagekeydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.
Trigger the click handler explicitly:
$("#s").keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
$("#go").click();
}
});
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