5

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.

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. 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'. 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. Commented Oct 15, 2014 at 13:16

4 Answers 4

8

You 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>
Sign up to request clarification or add additional context in comments.

Comments

4

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);
}

2 Comments

WHat about the enter key stuff? He wants it to only submit with mouseclick, not enterkey
Yeah, I commented on his question.
2

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".

1 Comment

This will work. Although I strongly discourage non-standard behavior - unless you have a very good, very specific use case where this functionality just simply must be altered.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.