title: Possible to Prevent Enter From Submitting a Form in JavaScript From Certain Input Fields Only
body: <p>I have a form for which <code>onkeyup</code> triggers the creation of a suggestions box for certain fields.  I am able to capture the keystrokes for the up arrow, for the down arrow and for escape and use them accordingly to move around in the suggestions box or close it.  I would like to use enter to move their selection into the input field that triggered the suggestions.  However, I seem to be unable to capture the enter keystroke.</p>
  <p>Control never enters my JavaScript suggest function.  Rather the form simply submits.  I've tried <code>noEnter</code> functions that return false on enter passed into <code>onkeypress</code> and <code>onkeydown</code> for the input element in question, no dice.  The form seems to grab control from all of the input event handlers and submit itself before any of them can do anything.</p>
  <p><strong>Does form submit override all other event handlers in fields, or is there something else going on here?  If it does, is there some way around that?</strong></p>
  <p>Here's the element in question (some values removed):</p>
  <pre><code>&lt;input type=&quot;text&quot; name=&quot;myInput&quot; id=&quot;myInputField&quot; size=&quot;22&quot; onkeypress=&quot;noEnter(event)&quot; onkeyup=&quot;suggest(event)&quot; onblur=&quot;hideSuggestions()&quot;&gt;
  </code></pre>
  <p>And here's the javascript to catch it:</p>
  <pre><code>    var evnt = (event ? event : window.event);
      
      var key = evnt.keyCode;
  
      if(key == 13) { // Handle Enter
          alert(&quot;ENTER PRESSED.&quot;);
          hideSuggestions();
          return false;
      }
  </code></pre>
  <p>The alert never fires.  Alerts put at the top of the function never fire either.  For the escape key however, when I place the exact same code in an if checking for <code>key == 27</code> the alert fires.</p>
  <p>There are other questions on almost the same topic, such as this one: <a href="https://stackoverflow.com/questions/923683/prevent-users-from-submitting-form-by-hitting-enter-2">Prevent Users from submitting form by hitting enter #2</a></p>
  <p>However, I am not using jquery and I only want to prevent enter from submitting for this one field.  But none of the other solutions mentioned in various questions seem to work.</p>
  <p>EDITTED: To explain where key is set.</p>