Sameera Liaynage
Sameera Liaynage

Reputation: 841

Disable submit a form in html when press Enter key

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.

Upvotes: 1

Views: 146

Answers (3)

Sameera Liaynage
Sameera Liaynage

Reputation: 841

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();
}'

Upvotes: 0

Adam Sinclair
Adam Sinclair

Reputation: 1649

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();
}

Upvotes: 0

user4067659
user4067659

Reputation:

press the tab key on your keyboard instead of enter.

Upvotes: 2

Related Questions