title: HTML Form - Execute script when pressing 'Enter', without submitting
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed">jQuery Event Keypress: Which key was pressed?</a>  </p>
  </blockquote>
  
  
  
  <p>OK, here's my situation <em>(I've solved this issue in the past, but since I'm not sure my approach was the best, I'm all ears for some advice)</em> :</p>
  
  <ul>
  <li>I've got a simple HTML form</li>
  <li>The purpose of this form is NOT to be submitted EVER</li>
  <li>The user normal clicks the (pseudo)<code>Submit</code> button, and the desired action is executed (via Javascript/Ajax, etc)</li>
  </ul>
  
  <p>The thing is :</p>
  
  <ul>
  <li>If the user hits <strong>enter</strong>, then the form <strong>is</strong> submitted, something we definitely DON'T WANT.</li>
  <li>What we want is to <em>catch</em> that particular keypress, and in that case, trigger the action that would normally be executed if the button was clicked.</li>
  </ul>
  
  <p>How would you go about this?</p>
  
  <hr>
  
  <h3>My Approach</h3>
  
  <hr>
  
  <p><strong>The Form</strong></p>
  
  <pre><code>&lt;form id="someId" action="#" method="post" 
        onsubmit="return false;" onkeypress="return enterSub(this,event);"&gt;
  ...
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>The Code</strong></p>
  
  <pre><code>function enterSub(inField, e) 
  { 
          var charCode;
  
          if(e &amp;&amp; e.which)
          {
              charCode = e.which;
          }
          else if (window.event)
          {
              e = window.event;
              charCode = e.keyCode;
          }
  
          if (charCode == 13) 
          {
              performThatAction; // My Main action
          }
   }
  </code></pre>