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?
- 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 ?Iansen– Iansen2014-08-12 11:45:29 +00:00Commented Aug 12, 2014 at 11:45
- Use something like preventDefault or return false in you ng-submit handler.Iansen– Iansen2014-08-12 11:46:15 +00:00Commented Aug 12, 2014 at 11:46
- I am not using ng-submit and there is no button type="submit".Rawat Raman– Rawat Raman2014-08-12 11:59:23 +00:00Commented Aug 12, 2014 at 11:59
Add a comment |
1 Answer
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?
1 Comment
Rawat Raman
Thanks for answer but I don't wanna break the by default functionality.