Create new account I forgot my password    

Shift Focus to the Next TextBox using the Enter Key using jQuery
Rating: 5 user(s) have rated this article Average rating: 5.0
Posted by: Suprotim Agarwal, on 7/12/2010, in category "jQuery and ASP.NET"
Views: this article has been read 12956 times
Abstract: In this short and simple article, we will see how to tab through the TextBoxes with the Enter Key using jQuery.

Shift Focus to the Next TextBox using the Enter Key using jQuery
 
In this short and simple article, we will see how to tab through the TextBoxes with the Enter Key using jQuery.
This article is a chapter from my EBook called 51 Recipes with jQuery and ASP.NET Controls. The chapter has been modified a little to publish it as an article.
Note that for demonstration purposes, I have included jQuery code in the same page. Ideally, these resources should be created in separate folders for maintainability. The code shown below has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4.
Let us quickly jump to the solution to shift focus to the next TextBox using the Enter Key.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head2" runat="server">
    <title>Shift Focus to the Next TextBox when user presses Enter</title>
    <script type="text/javascript"
        src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
    </script>
   
    <script type="text/javascript">
        $(function() {
            $('input:text:first').focus();
            var $inp = $('input:text');
            $inp.bind('keydown', function(e) {
                //var key = (e.keyCode ? e.keyCode : e.charCode);
                var key = e.which;
                if (key == 13) {
                    e.preventDefault();
                    var nxtIdx = $inp.index(this) + 1;
                    $(":input:text:eq(" + nxtIdx + ")").focus();
                }
            });
        });
    </script>
</head>
<body>
    <form id="form2" runat="server">
    <div class="smallDiv">
        <h2>Enter text and hit Enter to move to next text box</h2><br />
        <asp:TextBox ID="TextBox1" runat="server" /><br />
        <asp:TextBox ID="TextBox2" runat="server" /><br />
        <asp:TextBox ID="TextBox3" runat="server" /><br />
        <asp:TextBox ID="TextBox4" runat="server" /><br />
    </div>
    </form>
</body>
</html>
This example uses the bind() function that binds one or more events to a handler. The key is detected using the e.which
var key = e.which
 
If the key == 13, i.e the Enter key is pressed, the default behavior (postback) is suppressed using e.preventDefault() and instead the index of the current textbox is retrieved. We then add 1 (one) to the index, to shift the focus to the next textbox.
if (key == 13) {
        e.preventDefault();
        var nxtIdx = $inp.index(this) + 1;
        $(":input:text:eq(" +nxtIdx + ")").focus();
}
 
This is how the user tabs through the textboxes when the Enter Key is pressed. Some data entry operators find it easier to use the Enter Key to tab through the input controls!
Note: If you plan to add textboxes at runtime and want this code to continue to work with the dynamically added textboxes, use live() instead of bind(). Remember that in each call, you cannot bind more than one event to live().
See a Live Demo
 
Shift Focus using Enter Between TextBox and TextArea
 
One of my EBook readers, Leny mailed asking how to shift focus between a TextBox and TextArea. Well the code shown above will not work in the case of a TextArea, since to select a textarea, you need to use $(‘textarea’) and we are using var $inp = $('input:text'). The best way to achieve this requirement will be to add a class(cls) to the elements as shown below:
    <head id="Head1" runat="server">
    <title>Shift Focus to the Next TextBox when user presses Enter</title>
    <script type="text/javascript"
        src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
    </script>
   
    <script type="text/javascript">
        $(function() {
            $('input:text:first').focus();
            var $inp = $('.cls');
            $inp.bind('keydown', function(e) {
                //var key = (e.keyCode ? e.keyCode : e.charCode);
                var key = e.which;
                if (key == 13) {
                    e.preventDefault();
                    var nxtIdx = $inp.index(this) + 1;
                    $(".cls:eq(" + nxtIdx + ")").focus();
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="smallDiv">
        <h2>Enter text and hit Enter to move to next text box</h2><br />
        <asp:TextBox ID="tb1" runat="server" class="cls" /><br />
        <asp:TextBox ID="tb2" runat="server" class="cls" /><br />
        <asp:TextBox ID="tb3" TextMode="MultiLine" runat="server" class="cls" /><br />
        <asp:TextBox ID="tb4" runat="server" class="cls" /><br />
    </div>
    </form>
</body>
</html>
Observe the change in these two lines of code:
var $inp = $('.cls');
and
$(".cls:eq(" + nxtIdx + ")").focus();
 
We are using the class cls to only work with the elements we are interested in. Now you can tab between the textbox and the textarea using an Enter Key.
I hope you found this article useful and I thank you for viewing it. This article was taken from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls which contains similar recipes that you can use in your applications.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Carol on Wednesday, July 14, 2010 4:23 AM
abc: Your comment was deleted as it was anonymous, based on wrong facts and was misleading. If you feel there is an error, use the contact page http://dotnetcurry.com/Contact.aspx

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER