|
Shift Focus to the Next TextBox using the Enter Key using jQuery
|
|
Rating: 5 user(s) have rated this article
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.
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.