Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute function when enter is pressed if textbox has focus

Tags:

jquery

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.

like image 612
oshirowanen Avatar asked Jul 27 '11 13:07

oshirowanen


People also ask

How do you call a function when Enter is pressed?

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.

How do you trigger click event on pressing enter key?

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.

How can you avoid button click event when enter key is pressed in a text box?

Solution 1function (event) { if (event. which == 13 || event. keyCode == 13) { //code to execute here return false; } return true; }); Implement it according to your need.


1 Answers

$('#myTextbox').bind('keyup', function(e) {

    if ( e.keyCode === 13 ) { // 13 is enter key

        // Execute code here.

    }

});
like image 77
Seth Avatar answered Sep 21 '22 15:09

Seth