2

I have a form with multiple input boxes and when the first input box has focus and if I press Enter then route is changing and navigating somewhere. How can I stop it?

3
  • Most probably when you press enter the form submits... that's why it's navigating to another page. Please post your form code. Are using ng-submit ? Commented Aug 12, 2014 at 11:45
  • Use something like preventDefault or return false in you ng-submit handler. Commented Aug 12, 2014 at 11:46
  • I am not using ng-submit and there is no button type="submit". Commented Aug 12, 2014 at 11:59

1 Answer 1

3

The form is submitted hitting enter.
You can try adding an event that listen to the Enter keypressed like this:

<input ... ng-keypress="keyPressed($event)"> 

Then in your controller you can just prevent the default behavior for Enter key.

$scope.keyPressed = function(event) { if (event.which === 13){ event.preventDefault(); } } 

You can find more info in several other questions around SO

Submit form on pressing Enter with AngularJS
How to use a keypress event in AngularJS?

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

1 Comment

Thanks for answer but I don't wanna break the by default functionality.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.