Reputation: 29
I have tried every possible combination for the textarea to submit the value when enter key is pressed but none of the worked.I cant find the error. plz help.
$('.textarea').keyup(function(e){
if( e.which == 13 ){
$('#messageFrm').submit();
}
});
$('#messageFrm').submit(function(){
var message = $('.textarea').val();
$.post("handlers/messages.php",{msg:message,condition:"sendMessage"}, function(response){
if( response==1 ){
LoadChat();
document.getElementById('messageFrm').reset();
}
});
return false;
});
here's my jquery script. Actually on pressing enter the data is inserted on the database but the textarea still hve the message and a new line is added with enter key.
Upvotes: 0
Views: 85
Reputation: 458
You can use keydown
of jQuery to add a listener for textarea
and event.preventDefault()
to prevent add a new line.
You can see my pen: https://codepen.io/blogk/pen/vaPYwL?editors=1010
Upvotes: 0
Reputation: 229
After submitting form all you are doing to clear text area then why not just code to clear value of text area directly
For that just replace
document.getElementById('messageFrm').reset();
With
$('.textarea').val('');
Upvotes: 1