Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my webform, I have 10 textboxes. I need to focus on the next textbox when Enter/Return is pressed. Is there any way to achieve this by just using a single jquery function without writing 10 different function
Posted

1 solution

A quick search landed me on this OS answer[^] which has a link to an example which does what you want.

The link: http://www.latentmotion.com/downloads/enter-to-tab.html[^]

The source:
XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("input").not( $(":button") ).keypress(function (evt) {
            if (evt.keyCode == 13) {
                iname = $(this).val();
                if (iname !== 'Submit'){
                    var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select');
                    var index = fields.index( this );
                    if ( index > -1 && ( index + 1 ) < fields.length ) {
                        fields.eq( index + 1 ).focus();
                    }
                    return false;
                }
            }
        });
    });
</script>
<title>Untitled Document</title>

</head>

<body>
<form id="form1" name="form1" method="post" action="http://www.codeproject.com">

  <label>
    <input type="text" name="textfield" id="textfield" />
  </label>
  <label>
    <input type="text" name="textfield2" id="textfield2" />
  </label>
  <label>
    <input type="text" name="textfield3" id="textfield3" />
  </label>

  <label>
    <input type="submit" name="button" id="button" value="Submit" />
  </label>
</form>
</body>
</html>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900