title: difficaulty with preventDefault and submitting form
body: <p>I am working with a dynamical form. When i enter my name in the first textfield, and press enter, the next textfield pops up, requesting my email. however, i can click enter many times and the next textfield keeps popping up, which i only want to pop up once. The other problem, is that my next textfield, which requests my email, submits the form if i press enter twice, which i also want to prevent. Any solution?</p>
  
  <p>Javascript  </p>
  
  <pre><code>function addEvent(element, eventType, theFunction, capture) {
  if (element.addEventListener) {
      element.addEventListener(eventType, theFunction, capture);
  }
  else if (element.attachEvent) {
      element.attachEvent("on" + eventType, theFunction);
  }
  }
  
  
  function createEmailField() {
  if ((window.event &amp;&amp; window.event.keyCode == 13) || event.keyCode == 13) {
  
      var name = document.getElementById("name").value;
  
      if (name == "") {
          window.alert("Mata in ditt namn");
      }
      else if (name.search(/^[A-Za-z]+$/) == -1) {
          window.alert("Mata in ett namn med bokstäver");
      }
      else {
  
          var newDiv = document.createElement("div");
  
          var br = document.createElement("br");
          newDiv.appendChild(br);
  
          newDiv.appendChild(document.createTextNode("Hej " + name + "!"));
  
          var br = document.createElement("br");
          newDiv.appendChild(br);
  
          newDiv.appendChild(document.createTextNode("Hur når vi dig?"));
  
          var br = document.createElement("br");
          newDiv.appendChild(br);
  
          newDiv.appendChild(document.createTextNode("Epost:"));
  
          var br = document.createElement("br");
          newDiv.appendChild(br);
  
          var input = document.createElement("input");
          input.setAttribute("type", "text");
          input.setAttribute("id", "email");
          input.setAttribute("name", "email");
          newDiv.appendChild(input);
  
          var form = document.getElementById("form");
          form.appendChild(newDiv);
  
          var email = document.getElementById("email");
          addEvent(email, "change", createTextArea, false);
  
      }
  }
  }
  
  function createTextArea() {
  
  var email = document.getElementById("email").value;
  
  if (email == "") {
      alert("Mata in ditt email");
  }
  else if (email.search(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) == -1) {
      window.alert("Mata in ett korrekt email");
  }
  else {
      var newDiv = document.createElement("div");
  
      var br = document.createElement("br");
      newDiv.appendChild(br);
  
      newDiv.appendChild(document.createTextNode("Vad har du för fråga om vår verksamhet?"));
  
      var br = document.createElement("br");
      newDiv.appendChild(br);
  
      var textArea = document.createElement("textArea");
      textArea.setAttribute("id", "question");
      textArea.setAttribute("name", "question");
      textArea.cols = "30";
      textArea.rows = "5";
      newDiv.appendChild(textArea);
  
      var br = document.createElement("br");
      newDiv.appendChild(br);
  
      newDiv.appendChild(document.createTextNode("Vi kommer att svara dig via: " +   email));
  
      var br = document.createElement("br");
      newDiv.appendChild(br);
  
      var button = document.createElement("input");
      button.setAttribute("type", "submit");
      button.setAttribute("value", "Skicka");
      button.setAttribute("name", "Skicka");
      button.setAttribute("id", "submit");
      newDiv.appendChild(button);
  
      var form = document.getElementById("form");
      form.appendChild(newDiv);
  
  }
  }
  
  function prevent(event) {
  if ((window.event &amp;&amp; window.event.keyCode == 13) || event.keyCode == 13) {
      if (window.event) {
          window.event.returnValue = false;
      } else if (event.preventDefault) {
          event.preventDefault();
      }
  }
  }
  
  
  function init() {
  var name = document.getElementById("name");
  addEvent(name, "keyup", createEmailField, false);
  name.onkeypress = prevent;
  }
  
  window.onload = init;
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;title&gt;Inl1-3&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  &lt;script type="text/javascript" src="Uppg3.js" &gt;
  &lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;h2&gt; Frågeformulär &lt;/h2&gt;
  &lt;form id="form" method="post"     action="http://student.ts.mah.se/da123aht11/echoscript.php"&gt;
  &lt;div&gt;
      Vad heter du?... 
      &lt;br /&gt;&lt;input type="text" name="name" id="name" /&gt; 
  &lt;/div&gt;
  &lt;/form&gt;
  
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>