1

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.

0

3 Answers 3

2

press the tab key on your keyboard instead of enter.

Sign up to request clarification or add additional context in comments.

2 Comments

This is going to be the best answer unless you have a very good reason to break tested web conventions.
yes. but i want use enter for that task. thanks your repay my friend.
0

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

where should i put this second coding part?
onkeydown='if ((event.keyCode == 13 && document.getElementById("field_0").value!="") && event.which == 13) {document.getElementById("field_1").focus();event.preventDefault();}'
0

i got the answer. it should put in the input tag which we want.

onkeydown='if ((event.keyCode == 13 && document.getElementById("field_0").value!="") &&  event.which == 13){   
                document.getElementById("field_1").focus();
                event.preventDefault();
}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.