Doncho Doncho - 10 months ago 55
HTML Question

Switch text field on enter press

No answers that I have found online have worked.

When my page loads, I have my JS file select the first textfield; which works.
I then want it so that if the enter key is pressed while in a textfield it then proceeds to the next textfield. Right now I have...

$('input[type="text"]').keyup(function(e) {
if(e.keyCode == 13) {

which doesn't work.

and my inputs are...

<input type="text" name="username" id="username" autocomplete="off" />
<input type="text" name="password" id="password" autocomplete="off" />

I apologize if I've done something dumb, but I've been trying to figure this out for far too long.


You just need to point your code snippet inside document ready function. Like below

$(document).ready(function (){
     $('input[type="text"]').keyup(function(e) {
          var key_code = e.keyCode || e.which;
          if(key_code == 13)

To be conscious suggest to use nextAll('input[type="text"]').first().focus() if there have any other DOM before second text input field.