I want to have a chat box (textarea) where if user press Enter then it should submit the chat, and if user press Shift+Enter then it should enter in a new line.
I tried something but not able to figure out the exact keyup or keydown thing. The code I'm using at the moment is:
$("textarea").keydown(function(e){
if (e.keyCode == 13 && !e.shiftKey)
{
e.preventDefault();
}
});
jsFiddle
Also I want to get the \n
in place when Enter+Shift key is pressed.
EDIT
Issue with my code is:-
When i am check the content on client using using alert then it shows the next line. But when i am posting it my rails back end. Then its just a simple string. No new line thing is there.
This is how i am sending chats to rails server.
$.post("/incomingchat", { body:$("#chat_" + group_id).val() },
function(data){
// do something..
});
I've answered this before. It's a little tricky if the caret is not at the end of the textarea:
How do I detect "shift+enter" and generate a new line in Textarea?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With