Search code examples
jspenteronkeypress

How can the cursor goes to next text field when i press key Enter?


This is my login.jsp page,here i have 2 text fields when i enter user name in first text field and hits Enter key from keyboard it is not going to next text field.how can i do this? thanks for your help.

<body>         
<div id="wrapper"> 
    <table width="90%" border="0" align="center"  cellpadding="0"  cellspacing="0" > 
        <tr> 
            <td width="15%"><input name="username" type="text" id="username" value="username"/></td> 
            <td><input name="password" type="password" id="password" value="password"/></td> 
        </tr> 
        <tr><td>
                &nbsp;
            </td></tr>
        <tr> 
            <td align="right"  valign="bottom" ><input type="button"  name= "button"  id= "button"   value= "Submit"   /></td> 
        </tr> 
    </table> 
</div>      


Solution

  • To change focus to another inputtext people usually hit tab instead of enter..

    However you can do that with jquery event handler..

    $( "#your-username-input" ).keypress(function( event ) {
    //press enter button
    if ( event.which == 13 ) {
     $( "#your-other-input" ).focus();
    }