Submit form when enter key pressed using Prototype javascript
13,192
Solution 1
This is an example of the kind of thing you could use:
$('input').observe('keypress', keypressHandler);
function keypressHandler (event){
var key = event.which || event.keyCode;
switch (key) {
default:
break;
case Event.KEY_RETURN:
$('yourform').submit();
break;
}
}
Solution 2
It is the same as above but does not need to set a function:
$('input').observe('keypress', function(event){
if ( event.keyCode == Event.KEY_RETURN || event.which == Event.KEY_RETURN ) {
// enter here your code
Event.stop(event);
}
});
Related videos on Youtube
![Mindey I.](https://lh6.googleusercontent.com/-TQv9hQe7bZo/AAAAAAAAAAI/AAAAAAAADq0/6cYdoUDaUhY/photo.jpg?sz=256)
Author by
Mindey I.
Co-founder and CEO of https://coinbase.com Personal blog at http://brianarmstrong.org Lover of software engineering.
Updated on May 02, 2022Comments
-
Mindey I. about 2 years
I've seen some other questions around this, but none for Prototype.
I have a form without a submit button (uses a styled link that calls some javascript).
What's the best way to detect a enter keypress in all the input fields and submit the form?
Thanks!