I have an HTML form with several text fields. So, I want to move cursor from a text field to next text field when I press the enter key, but the form should not be submitted. How can I do this? If there is a code example it may more helpful to me.
3 Answers
press the tab key on your keyboard instead of enter.
2 Comments
Brad Wells
This is going to be the best answer unless you have a very good reason to break tested web conventions.
Sameera Liaynage
yes. but i want use enter for that task. thanks your repay my friend.
I'm using this function to do what you ask for:
$(document).on("keypress", ".TabOnEnter" , function(e)
{
if( e.keyCode == 13 )
{
var nextElement = $('[tabindex="' + (this.tabIndex+1) + '"]');
console.log( this , nextElement );
if(nextElement.length )
nextElement.focus()
else
$('[tabindex="1"]').focus();
}
});
Use the 'TabOnEnte' class on the fields you want this class to apply on.
Also to prevent user from submittin with enter key:
if (e.which == 13) {
e.preventDefault();
}
2 Comments
Sameera Liaynage
where should i put this second coding part?
Sameera Liaynage
onkeydown='if ((event.keyCode == 13 && document.getElementById("field_0").value!="") && event.which == 13) {document.getElementById("field_1").focus();event.preventDefault();}'