title: Simulate an enter key press in Javascript with GreaseMonkey
body: <p>What I want to do is fill up an input field, focus on it and press Enter to submit it. All of this automatically with GreaseMonkey on a specific page.</p>
  
  <p>Here is what I have so far, the field is filled up, but I can't get the keypress to work.</p>
  
  <pre><code>var my_email = "test@maildrop.cc";
  
  var field = document.getElementById('email');
  
  var key_to_press = new Array;
  key_to_press["key"] = "Enter";
  key_to_press["keyCode"] = "13";
  var ev = new KeyboardEvent("pressEnter", key_to_press);
  
  field.value = my_email;
  
  setTimeout(function() {
    field.focus();
  }, 2000);
  
  setTimeout(function() {
    field.dispatchEvent(ev);
  }, 2000);
  </code></pre>
  
  <p>Here is some of the HTML from the page:</p>
  
  <pre><code>&lt;div class="form-group no-label required"&gt;    
      &lt;label class="control-label" for="invite-email"&gt;&lt;/label&gt;
      &lt;div class="input-group"&gt;
          &lt;input id="hiddenKid" type="hidden" value="BUXB4U"&gt;&lt;/input&gt;
          &lt;input id="email" class="form-control" type="email" placeholder="E-mail Address" data-type="email" name="invite-email"&gt;&lt;/input&gt;    
      &lt;/div&gt;    
  &lt;/div&gt;
  &lt;div id="submit_email" class="btn btn-submit-invite"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Notice that there is also a button to submit the email, I could use it but I really don't know how to, since it is just a div probably handled by some other JS from the website...</p>
  
  <p>So does anyone know how can I make the enter keypress happen, or maybe click the button?</p>