Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery call function if Enter hit

Tags:

jquery

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?

like image 950
PHP Ferrari Avatar asked Apr 04 '13 05:04

PHP Ferrari


People also ask

How do you check if enter key is pressed in jQuery?

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.

How do you check if Enter was pressed?

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.

How do you call a function in Enter?

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.

What is Keyup and Keydown in jQuery?

Definition and Usagekeydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.


1 Answers

Trigger the click handler explicitly:

$("#s").keypress(function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    $("#go").click();
    }
});
like image 171
Barmar Avatar answered Sep 17 '22 18:09

Barmar