12
How to make the enter key fail Submit on a form?
Example: in an input by pressing the enter nothing happens.
Today I press the enter, the browser gives a Submit, as and had clicked on send button.
12
How to make the enter key fail Submit on a form?
Example: in an input by pressing the enter nothing happens.
Today I press the enter, the browser gives a Submit, as and had clicked on send button.
22
You can replace Handler for standard behavior:
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
Or, on the Form tag:
<form onsubmit="return false;">
8
So too does:
$('#form').bind('submit', false);
$('button#submit').click(function()
{
$('#form').submit();
});
This will only send the form when you click the Submit button.
0
Alternative with pure Javascript:
document.addEventListener("keydown", function(e) {
if(e.keyCode === 13) {
e.preventDefault();
}
});
In the first line we are adding an event of keydown to the document
That is, whenever a key is pressed, we will have an event being fired at the DOM
Then we check if the pressed key is enter, which has the code 13, with the comparison of keycode
If enter, we prevent the default event with preventDefault, that is sending the form to the server
0
Use event.preventDefault();
This will prevent the default event from occurring.
The use of return false
within a jQuery event handler is effectively the same as calling event.preventDefault()
.
See this question for a better explanation: https://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false
Browser other questions tagged javascript jquery html html5
You are not signed in. Login or sign up in order to post.
Ah, the guy went faster ;)
– Wallace Maxters
Leeevemente @Wallacemaxters ;)
– OnoSendai