Skip to content
Advertisement

How do I detect “shift+enter” and generate a new line in Textarea?

Currently, if the person presses enter inside the text area, the form will submit.
Good, I want that.

But when they type shift + enter, I want the textarea to move to the next line: n

How can I do that in JQuery or plain JavaScript as simple as possible?

Advertisement

Answer

Better use simpler solution:

Tim’s solution below is better I suggest using that: https://stackoverflow.com/a/6015906/4031815


My solution

I think you can do something like this..

EDIT : Changed the code to work irrespective of the caret postion

First part of the code is to get the caret position.

Ref: How to get the caret column (not pixels) position in a textarea, in characters, from the start?

JavaScript

And then replacing the textarea value accordingly when Shift + Enter together , submit the form if Enter is pressed alone.

JavaScript

Here is a demo

Advertisement