48

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/

Neithan Max
  • 11,004
  • 5
  • 40
  • 58
Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92

6 Answers6

80

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;
  }
});
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 1
    This answers the question. Keep in mind, that there are other ways to get a line break in your textarea, such as pasting or drag&drop. In this case you might want to also handle the change event, and replace the \r\n instead of preventing the event. – Oliver Schimmer Mar 07 '18 at 18:40
  • I can confirm that `.preventDefault()` works in plain JavaScript too. – mythofechelon Oct 02 '20 at 19:24
  • @TalKohavy Jquery is a Javascript library is it not? And as noted in the comments above *.preventDefault() works in plain JavaScript too*. **Also if you look at his jsfiddle, it is written in Jquery.** – Scary Wombat Aug 10 '21 at 00:15
5

Below code is to prevent to getting resize the "textarea", avoid scroll bar inside textarea, and also prevent to get into next line when enter key is pressed.

style.css

textarea {height:200px; width:200px;overflow:hidden;resize: none;}

index.html

<textarea></textarea>

Script.js

$("textarea").keydown(function(e){
    // Enter pressed
    if (e.keyCode == 13)
    {
        //method to prevent from default behaviour
        e.preventDefault();
    }
});
nano dev
  • 335
  • 4
  • 6
3

React: textarea w/ value and onChange event

const PreventTextAreaEnter = () => {
   const [comment, setComment] = useState();

   const handleTextArea = (e) => {
      console.log({e}) // Destructure to get a more accurate log 

      // Return if user presses the enter key
      if(e.nativeEvent.inputType === "insertLineBreak") return;

      setComment(e.target.value);
   };

   return (
    <>
      <textarea value={comment} onChange={ (e) => { handleTextArea(e) } />
    </>
   )
  }
Jason
  • 813
  • 11
  • 13
0
    $(".Post_Description_Text").keypress(function(event) {

      if (event.which == 13) {        

        alert("Function is Called on Enter");

          event.preventDefault(); //Add this line to your code

       }

   });
Nirav Thakar
  • 166
  • 1
  • 10
0

For Angular Users

While there are existing working solutions, if anyone comes across this question using Angular, you can disable new lines with the following:

Add <textarea ng-trim="false" ng-model='your.model' ...

In your controller, add:

$scope.$watch('your.model', function(newvalue, oldvalue) {
    if (newvalue && newvalue.indexOf('\n') > -1) {
       $scope.your.model = oldvalue;
    }
});
Matt Goodrich
  • 4,875
  • 5
  • 25
  • 38
-9

use the input tag instead of textArea tag in your HTML

FelixSFD
  • 6,052
  • 10
  • 43
  • 117