I have a js event on enter keypress which works fine. But after that event it also submits the form which has a submit button. How do I stop that submit button from getting focus after the previous keypress event?
EDIT: I DONT WANT THE SUBMIT BUTTON TO SUBMIT ON ENTER, ONLY ON CLICK.
I have a js event on enter keypress which works fine. But after that event it also submits the form which has a submit button. How do I stop that submit button from getting focus after the previous keypress event?
EDIT: I DONT WANT THE SUBMIT BUTTON TO SUBMIT ON ENTER, ONLY ON CLICK.
Share Improve this question edited Apr 30, 2010 at 18:52 zsharp asked Apr 30, 2010 at 18:44 zsharpzsharp 13.8k29 gold badges90 silver badges156 bronze badges 3- 1 Okay, so replace your submit with a button. So instead of <input type="submit".." get an <input type="button".. and attach an onclick event to it. In the onclick handler, do stuff and call the submit() method of the form. – nc3b Commented Apr 30, 2010 at 18:54
- 1 I feel like this is worth repeating: I strongly discourage this kind of non-standard behavior - unless you have a very good, very specific use case where this functionality just simply must be altered. Aside from that, the answers here are correct: your keypress event handler should simply return 'false'. – anonymous coward Commented Apr 30, 2010 at 18:57
- @anonymouscoward - many embedded or terminal-like systems do not have tab keys for moving between fields. Users have been conditioned to hit ENTER after typing in data for a field to advance to the next field. I agree that this is non-optimal, but it is what it is. – StingyJack Commented Oct 15, 2014 at 13:16
4 Answers
Reset to default 8You can do this with java script are as follows;
<script type="text/javascript">
function stopReloadKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13) {
return false;
}
}
document.onkeypress = stopReloadKey;
</script>
Or Using Jquery you can do this by,
<script type="text/javascript">
$(document).ready(function() {
$("form").bind("keypress", function(e) {
if (e.keyCode == 13) {
return false;
}
});
});
</script>
Not sure if I understand correctly, but if you are trying to prevent the form from getting submitted:
Attach an onsubmit event and return false from it.
<form onsubmit="submit_handler();"></form>
[...]
function submit_handler()
{
//do stuff
return (false);
}
Typically the answer to javascript questions of the type "How do I keep my form from submitting after I ...." is "make your javascript function return false
".
any enter fires the submit event you need to override it - here a short example code (5 lines) how to do it http://www.bloggingdeveloper.com/post/Disable-Form-Submit-on-Enter-Key-Press.aspx