Disable New Line in Textarea when Pressed ENTER

Hassan Sardar picture Hassan Sardar · Sep 13, 2013 · Viewed 55.7k times · Source

I am calling a function whenever someone press enter in the textarea. Now I want to disable new line or break when enter is pressed.

So new line should work when shift+enter is pressed. In that case, the function should not be called.

Here is jsfiddle demo: http://jsfiddle.net/bxAe2/14/

Answer

Scary Wombat picture Scary Wombat · Sep 13, 2013

try this

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

update your fiddle to

$(".Post_Description_Text").keydown(function(e){
if (e.keyCode == 13 && !e.shiftKey)
{
  // prevent default behavior
  e.preventDefault();
  //alert("ok");
  return false;
  }
});