Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Target button with enter key when input field is focused

Tags:

html

jquery

I have an input field and a login button. I want to allow users to hit the "enter" key to click the button when the input field is focused. How would I do this with jQuery?

<input type="text" size="10" id="loginUserID" name="username"> <input type="button" value="Login" name="loginBtn" id="loginBtn"> 
like image 1000
user547794 Avatar asked Oct 29 '11 06:10

user547794


People also ask

How do you trigger a button on a 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 do I move focus on next field when Enter is pressed in jQuery?

They both use a custom jQuery selector that I add called :focusable to select all focusable element (including links): // register jQuery extension jQuery. extend(jQuery. expr[':'], { focusable: function (el, index, selector) { return $(el).is('a, button, :input, [tabindex]'); } }); $(document).

How do you trigger HTML button after hitting Enter button in textbox using JavaScript?

We can do it by using “keyup”, “keydown”, or “keypress” event listener on textbox depending on the need. When this event is triggered, we check if the key pressed is ENTER or not.

How can input focus in jQuery?

Using jQuery With jQuery, you can use the . focus() method to trigger the “focus” JavaScript event on an element. This method is a shortcut for . trigger("focus") method.


1 Answers

$('#loginUserID').keypress(function(event){   if(event.keyCode == 13){     $('#loginBtn').click();   } }); 

Demo: http://jsfiddle.net/Bhf5a/

like image 122
AlienWebguy Avatar answered Oct 03 '22 13:10

AlienWebguy