Use the Enter key to go to the inputs

Can I use the enter key to go to the next input field on the form? I also want to use the tab, but the enter key will be nice too.

Fyi. I have several text fields, and I need to use the enter key to return when I type them. Will this be a conflict?

Thank. Erik

+3
source share
2 answers

If you must add a class called "TabOnEnter" to the fields in which you want to include a loop.

$(document).on("keypress", ".TabOnEnter" , function(e)
  {
    //Only do something when the user presses enter
    if( e.keyCode ==  13 )
    {
       var nextElement = $('[tabindex="' + (this.tabIndex+1)  + '"]');
       console.log( this , nextElement ); 
       if(nextElement.length )
         nextElement.focus()
       else
         $('[tabindex="1"]').focus();  
    }   
  });

//Hidden inputs should get their tabindex fixed, not in scope ;)
//$(function(){ $('input[tabindex="4"]').fadeOut();  })

Not as cute as the previous answer, but now it works:

http://jsfiddle.net/konijn_gmail_com/WvHKA/

HTML (tabindex), . .

+5

( , ):

$(".myTextareas").keypress(function(e) {
    if(e.which == 13) {
        $(this).next('.myTextareas').focus();
    }
});
0

All Articles