I am trying to use various methods to get a form to submit when I press enter. I know that it works with an input field, but since it will be a comment, it should be a text area.
This is what I currently have for submitting a form using a button.
$('.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('<li><img class="commentUserImg" src="' + profilepic + '"><div class="commentUser">' + username + '</div><div class="commentText">' + dataString + '</div></li>'); $('.messageTextarea').val(''); // Remove comment from TextArea } }); return false; });
It works as it should. I want to remove the submit button and just submit the form when the user presses the enter key. I know some people advise against this, but users on this website will be used to log in to Facebook, etc.
I tried such methods, but it doesn't seem to work.
$('.messageTextarea').keydown(function() { if (event.keyCode == 13) { this.form.submit(); return false; } });
Here is my form code
<form> <textarea class="messageTextarea" onfocus="if(this.value==this.defaultValue)this.value=\'\';" onblur="if(this.value==\'\')this.value=this.defaultValue;">Write your comment here...</textarea> <input type="submit" data-the_id="' + theid + '" name="submit" class="messageSubmit" id="submit_btn" value="Submit your comment"> </form>
Any help would be great. Also, if anyone knows how to add an if function that will prevent the form from being submitted with the current default value in Textarea, that would be awesome. Currently, with the way textarea is configured now, if you just click submit, it will post your comment here ...
thanks
EDIT: Can the work work ... Having a button to send, as usual, but hidden, and when you press the enter button, does it call this button? But then ... I faced the same input problem, I do nothing in the text box, except to break into a new line ...