Shift Focus to the Next TextBox using the Enter Key using jQuery
Posted by: Suprotim Agarwal ,
on 7/12/2010,
in
Category jQuery and ASP.NET
Abstract: In this short and simple article, we will see how to tab through the TextBoxes with 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.
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().
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn