zsharp
zsharp

Reputation: 13756

How do I disable form submit with enter key?

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.

Upvotes: 5

Views: 19511

Answers (4)

Kailas Mane
Kailas Mane

Reputation: 1935

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>

Upvotes: 8

ariel
ariel

Reputation: 9

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

Upvotes: 0

Jacob Mattison
Jacob Mattison

Reputation: 51052

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

Upvotes: 2

nc3b
nc3b

Reputation: 16220

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

Upvotes: 4

Related Questions