title: Submit form using enter without triggering page refresh
body: <p>I know it is possible to submit a form using the enter key, but doing so refreshes the page.</p>
  
  <p>I am making an interactive game where the user's responses are added to a <code>div</code> whenever he makes a response (submits the form).</p>
  
  <p>However, by refreshing the page, the current text in the <code>div</code> disappears, and thus this is NOT desired.</p>
  
  <p>Is it possible to trigger a script using the enter key when you submit a form WITHOUT the page refreshing?</p>
  
  <p>Illustrated example: </p>
  
  <pre><code>&lt;form action="tdat.php" method="post" onsubmit="return answer(event)"&gt;
      &lt;input type="text" name="userInput" id="searchTxt" maxlength="50" size="50"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This is the form that I am using. I tried making a function <code>answer(e)</code> to respond to the user's input, and display the text to the <code>div</code>.</p>
  
  <p>However, the code <code>answer(e)</code> does not run, and the page also refreshes. How should I combat this? Thanks in advance.</p>
  
  <p>UPDATE:</p>
  
  <p>To those wondering the contents of the event in <code>answer(e)</code> it is as follows:</p>
  
  <pre><code>function answer(e) {
      if (e.keyCode == 13) {
          addText()
          responseCheck()
          document.getElementById("searchTxt").value = ""
          autoScroll()
      }
  }
  </code></pre>
  
  <p><code>addText()</code> is the one that appends the <code>HTML</code> to the <code>div</code>, <code>responseCheck()</code> is the function that calculates the response and the rest should be obvious. </p>