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?
Asked
Active
Viewed 1,044 times
2
-
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 Aug 12 '14 at 11:45
-
Use something like preventDefault or return false in you ng-submit handler. – Iansen Aug 12 '14 at 11:46
-
I am not using ng-submit and there is no button type="submit". – Rawat Raman Aug 12 '14 at 11:59
1 Answers
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?
-
Thanks for answer but I don't wanna break the by default functionality. – Rawat Raman Aug 12 '14 at 12:01