-2

I am working on a message system where you type a message such as "Hello" and it will POST it to the server and the user will be able to see it.

Is there a way where if a user presses shift+enter they can create a new paragraph?

How a message looks currently:

Hello John, How are you?

How a message should look (By pressing shift+enter after the comma):

Hello John,

How are you?


PS

I am very sorry for not creating some sort of fiddle here, but I couldn't really think of what to put in it or how I could demonstrate it.

Thanks so much!

Galliger
  • 45
  • 8
  • 2
    https://stackoverflow.com/questions/6014702/how-do-i-detect-shiftenter-and-generate-a-new-line-in-textarea –  Aug 30 '17 at 13:06
  • Don't have much idea about it but you will need to create short cuts using jquery ... – Just_Do_It Aug 30 '17 at 13:06
  • @Just_Do_It That's what I was thinking, I would need to somehow bind shift+enter to make a new line or something... But I have no idea on how to do this? – Galliger Aug 30 '17 at 13:10

2 Answers2

0

based on this answer you could use this

  
$('#paragraph').keyup(function (event) {
    if (event.keyCode == 13) {
        var content = this.value;        
        if(event.shiftKey){
            this.value = content.substring(0, this.value.length) + "\n";
            event.stopPropagation();
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="paragraph" rows="4" cols="30"></textarea>
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37
  • 1
    I still want enter to submit the form. (So SHIFT+Enter creates a new line and enter will submit the form) Thank you. – Galliger Aug 30 '17 at 13:12
0

HTML

<textarea rows="4" cols="50"></textarea>

JS

$("textarea").keydown(function(e){
    if (e.keyCode == 13 && !e.shiftKey)
    {
        e.preventDefault();
    }
});

TRY HERE

Amit Arya
  • 142
  • 2
  • 13
  • 1
    I still want enter to submit the form. (So SHIFT+Enter creates a new line and enter will submit the form) Thank you. – Galliger Aug 30 '17 at 13:13