title: Submit form when enter key is pressed
body: <p>I currently have a live chat room up and running and just asked some users what I can do to make the chat room even better. One thing that was asked multiple times was this...</p>
  
  <p><em>"Can you please add a feature where pressing the enter button will send my message instead of adding a new line in the text box?"</em></p>
  
  <p>I tried to apply some jQuery from a different post on StackOverflow but couldn't get it to work. Here is my current code...</p>
  
  <p>jQuery:</p>
  
  <pre><code>&lt;!-- jQuery for submitting form --&gt;
  &lt;script&gt;
  var msgForm = false;
  
        $(function () {
  
      $('.expand').keyup(function(event) {
          if (event.keyCode == 13) {
          this.form.submit();
              msgForm = true;
              return true;
           }
      });
  
      if( msgForm = true ) {
          $('#formSend').on('submit', function (e) {
  
            e.preventDefault();
  
            var formData = $('form').serialize();
            $('.expand').prop("disabled", true)
  
            $.ajax({
              type: 'post',
              url: 'send.php',
              data: formData,
              success: function () {
                $(".expand").val('');
                $('.expand').prop("disabled", false) 
              }
            });
  
          });
          }
  
        });
  
      &lt;/script&gt;
  </code></pre>
  
  <p>Here ^^ an Ajax request is made to send the message so no page refresh is involved like a usual form.</p>
  
  <p>Here is the HTML markup for the form if needed...</p>
  
  <pre><code>  &lt;form action="send.php" method="post" name="formSend" id="formSend" /&gt;
       &lt;textarea id="styled" class="expand" name="msg" placeholder="Your Message Here" onfocus:"setbg(\'#e5fff3\');"required&gt;&lt;/textarea&gt;
       &lt;input type="submit" name="submit" value="Send" class="send" /&gt;
    &lt;/form&gt;
  </code></pre>