15

I have a form with a text area and hitting the enter key submits my form. How can I make it to add a new line character instead of a form submit.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
Khizar
  • 2,288
  • 5
  • 32
  • 53

7 Answers7

32
$('textarea').keypress(function(event) {
   if (event.which == 13) {
      event.stopPropagation();
   }
});​

JSFiddle Demo

N.Jain
  • 3
  • 4
Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62
  • 1
    well i was thinking to answer it first but i had a confusion about `/n` or `\n` ;) – Muhammad Talha Akbar Dec 24 '12 at 10:57
  • 2
    This (the previous version) actually adds a new line to the end of the text – but what if you're typing in the middle of the text? I've edited this accordingly. — One issue is still unfixed about this though, as Jack Cood mentions: adding a new line in the last visible row means you have to adjust the scroll bar. — I'm just _super_ befuddled that this doesn't work out of the box. Such a bug! – WoodrowShigeru Nov 14 '15 at 18:00
  • Great solution that simply works! However, I would still recommend doing it the vanilla JS way, i.e. `element.addEventListener('keypress', event => ...)'` – ton Feb 27 '19 at 07:13
3

Previous answers don't handle splitting the current value of the textarea and simply appends a new line character. The following stops the event from bubbling up to the form and still allows typical textarea behavior on 'enter'.

$('textarea').keydown(function(event) {
  if (event.keyCode === 13) {
    event.stopPropagation();
  }
});
David Rice
  • 1,111
  • 1
  • 11
  • 24
2

you can try this code:

$(document).ready(function() {
  $("textarea").keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
2

This should help

$('#myProblematicForm textarea').keypress(function(event) {
  if (event.which == 13) {
    event.preventDefault();
    this.value = this.value + "\n";
  }
});

For what it's worth, I'm using Chrome on OS X and Enter inserts a \n in a textarea for me and does not submit forms by default.

maček
  • 76,434
  • 37
  • 167
  • 198
  • ok well, i m also using Google Chrome But On Windows, It does not adds line break as default! – Muhammad Talha Akbar Dec 24 '12 at 11:07
  • @maček: your solution works, but I got a problem with Chrome. If textarea is scrollable, when I press enter key, the scrollbar doesn't work, I mean it doesn't scroll to new line. Any ideas for this problem? – Jack Cood Sep 22 '14 at 02:37
  • This adds a new line to textarea but form is still submitted. `return false` is needed. But with this solution I cannot add a new line anywhere else than in the end of the textarea. – Panu Haaramo Dec 06 '21 at 11:14
2
$(document).ready(function(){
$("#text").keypress(function(event) {   
   if (event.which == 13) { 
      var s = $(this).val();
      $(this).val(s+"\n");  //\t for tab
   }
});      
});

This can be done by jquery code that I have given above. Must notice that the use of ready function is necessary when you write above script before the codes of the HTML input field, and not necessary when you write it after the input field codes.If it not working try to use \t in place of \n it will work.

  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Please read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. – thewaywewere Jun 10 '17 at 12:16
  • Thanks for the suggestions @thewaywewere , see my edited answer, hope it might help you. – Abhinav Chauhan Jun 13 '17 at 09:55
1
$(document).ready(function() {
  $('textarea').keydown(function(event){
    if (event.keyCode == 13) {
      event.preventDefault();
      var s = $(this).val();
      $(this).val(s+"\n");
    }
  });
});
Andy
  • 61,948
  • 13
  • 68
  • 95
1

This worked for me

$(document).keypress(function(e) {
if(e.which == 13) {
    if(document.activeElement.tagName != "TEXTAREA") {
        e.preventDefault();
    };
  }
})