Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intercept carriage return in a textarea

Tags:

jquery

How do you catch a carriage-return in a textarea and do a form post instead of a newline in the textarea?

like image 270
Phillip Senn Avatar asked Nov 02 '09 18:11

Phillip Senn


People also ask

How do I put carriage return in textarea?

To add line breaks to a textarea, use the addition (+) operator and add the \r\n string at the place where you want to add a line break, e.g. 'line one' + '\r\n' + 'line two' . The combination of the \r and \n characters is used as a newline character. Here is the HTML for the examples in this article. Copied!

How to Disable enter key in textarea?

Disabling enter key for the form keyCode === 13 || e. which === 13) { e. preventDefault(); return false; } }); If you want to prevent Enter key for a specific textbox then use inline JS code.


1 Answers

Capture the keystroke, verify if it is enter, and then look for the parent form element and submit it:

$('#textAreaId').keydown(function (e) {
  var keyCode = e.keyCode || e.which;

  if (keyCode == 13) {
    $(this).parents('form').submit();
    return false;
  }
});

Check the above example here.

like image 140
Christian C. Salvadó Avatar answered Oct 06 '22 07:10

Christian C. Salvadó