Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I only allow shift+enter to return new line in text area?

In a default behavior, the textarea "press" enter will become new line, but I don't want to having a new line, I want the user press shift+enter, instead. How can I do so? or... ... can I return the textarea enter event before it actually fire to the text area?

like image 707
DNB5brims Avatar asked Apr 30 '11 15:04

DNB5brims


People also ask

How do you allow new line in textarea?

\n is the linefeed character literal (ASCII 10) in a Javascript string. <br/> is a line break in HTML. Many other elements, eg <p> , <div> , etc also render line breaks unless overridden with some styles.

How do I detect shift-enter and generate a new line in textarea?

To detect shift+enter and generate a new line in text area with JavaScript, we can check for shift and enter key presses in the event object. textArea. onKeyPress = (e) => { if (e. keyCode === 13 && e.

How do you text a newline?

To start a new line of text or add spacing between lines or paragraphs of text in a worksheet cell, press Alt+Enter to insert a line break.


1 Answers

$("textarea").keydown(function(e){
    // Enter was pressed without shift key
    if (e.key == 'Enter' && !e.shiftKey)
    {
        // prevent default behavior
        e.preventDefault();
    }
});

Try the jsFiddle.

like image 178
BrunoLM Avatar answered Oct 21 '22 19:10

BrunoLM