Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit value on pressing Enter in textarea and pressing Shift+Enter should go to next line

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..
    });
like image 648
Mohit Jain Avatar asked Sep 08 '11 08:09

Mohit Jain


1 Answers

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?

like image 50
Tim Down Avatar answered Sep 28 '22 07:09

Tim Down