Note that there are some explanatory texts on larger screens.

plurals
  1. POForm submitting when pressing enter in Textarea
    primarykey
    data
    text
    <p>I've been trying various methods to get a form to submit when hitting the enter key. I know it works with an input field, but because this is going to be a comment, it needs to be a text area. </p> <p>This is what I currently have for the form to submit with a button. </p> <pre><code>$('.messageSubmit').live('click', function(){ var name = $(this).siblings('.messageTextarea').val(); var theid = $(this).attr('data-the_id'); var dataString = name; $.ajax({ dataType: 'json', url: "https://api.instagram.com/v1/media/"+$(this).attr('data-the_id')+"/comments?"+hash, type: "POST", data: "text="+dataString, success: function(data) { // finish load console.log(data, dataString, 'fail'); }, error: function(data) { var username = JSON.parse(localStorage.getItem('iguser')); var profilepic = JSON.parse(localStorage.getItem('iguserimg')); //console.log(data, dataString, 'succ'); $('.box[data-the_id="' + theid + '"]').children('.postMessage').children('.messageComments').append('&lt;li&gt;&lt;img class="commentUserImg" src="' + profilepic + '"&gt;&lt;div class="commentUser"&gt;' + username + '&lt;/div&gt;&lt;div class="commentText"&gt;' + dataString + '&lt;/div&gt;&lt;/li&gt;'); $('.messageTextarea').val(''); // Remove comment from TextArea } }); return false; }); </code></pre> <p>It works like it should. I want to remove the submit button and just have the form submit when a user hits the enter key. I know some people advise against this, but the users on this website will be used to hitting enter from Facebook and such. </p> <p>I've tried methods such at this, but it none seem to work. </p> <pre><code>$('.messageTextarea').keydown(function() { if (event.keyCode == 13) { this.form.submit(); return false; } }); </code></pre> <p>Here is my form code</p> <pre><code>&lt;form&gt; &lt;textarea class="messageTextarea" onfocus="if(this.value==this.defaultValue)this.value=\'\';" onblur="if(this.value==\'\')this.value=this.defaultValue;"&gt;Write your comment here...&lt;/textarea&gt; &lt;input type="submit" data-the_id="' + theid + '" name="submit" class="messageSubmit" id="submit_btn" value="Submit your comment"&gt; &lt;/form&gt; </code></pre> <p>Any help would be great. Also, if anyone knows how to add an if function that will prevent the form from submitting with the current default value in the Textarea, that would be awesome. Currently, with how the textarea is set up now, if you just hit submit, it will submit <em>Write your comment here...</em></p> <p>Thanks</p> <p><strong>EDIT: Could a work around be... Having an button to submit, like normal, but have it hidden, and when you hit enter it triggers a call for that button? But then... I'd run into the same problem of enter doing nothing in the textarea except break into a new line...</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload