Here’s my jQuery code
$('.chat_bar_message_input').keypress(function(e) {
if(e.keyCode == 13) {
var message_body = $('.chat_bar_message_input').val();
$.post("assets/scripts/chat/message_parse.php", {message: message_body}, function(data) {
alert(data);
})
$('.chat_bar_message_input').val("")
}
});
and here’s the HTML
<textarea class ="chat_bar_message_input"></textarea>
The code is for a chat bar application I am working on, and this code is supposed to set the value of the textarea to null when it is submitted. However, because it runs the code when the enter button is pressed it runs the code, clears the value of the textarea then adds the line break in the textarea. How can I prevent this?
All help is much apreciated.
Thanks.
Use
e.preventDefault()which will stop the browser action to enter new line.you can read about it here