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

DNB5brims picture DNB5brims · Apr 30, 2011 · Viewed 25k times · Source

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?

Answer

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

Try the jsFiddle.