Reputation: 3304
In a ASP website i want to navigate between textbox using enter keys so that when i press enter key the focus should go for next textbox.
What I did is in asptextbox used onkeypress function as shown below
<asp:TextBox ID="Textbox1" runat="server" onkeypress="return EnterEvent(event);"></asp:TextBox>
And in javascript
function EnterEvent(e) {
if (e.keyCode == 13) {
document.getElementById("Textbox2").focus();
}
}
</script>
It works fine. But in case Textbox2 is a multiline textbox(if i use TextMode="MultiLine" for a asp textbox) then focus will be placed on Textbox2 but it will be placed one extra line below in textbox(focus placed in 2 nd line of multiline textbox). How it can be solved?
Upvotes: 2
Views: 1678
Reputation: 1838
Try this script for enter key navigation through controls..
<script type = "text/javascript" >
function pageLoad(sender, args) {
var inputs = $(':input,select').keydown(function (e) {
if (e.which == 13) {
if (e.target.id == '') {
return;
}
e.preventDefault();
var nextInput = inputs.get(inputs.index(this) + 1);
if (nextInput) {
nextInput.focus();
nextInput.select();
}
}
});
}
</script>
If the above script is not suitable for your case you can just try changing onkeypress
to onkeydown
..You can either use take any one of the cases,not both at a time..
<asp:TextBox ID="Textbox1" runat="server" onkeydown="return EnterEvent(event);"></asp:TextBox>
You can understand the difference between keydown and keypress by refering the link below..
Difference between Keypress and keydown...
Upvotes: 1