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.
- no iam working with Spring mvc, but this is a simple html form.Khizar– Khizar2012-12-24 10:51:44 +00:00Commented Dec 24, 2012 at 10:51
- well buddy shift+enter as default work as what you wantTalha Akbar– Talha Akbar2012-12-24 10:51:54 +00:00Commented Dec 24, 2012 at 10:51
- i know shift+enter does that but a normal user does not usually know that, i want to disable it and add new line character by using js or jquery.Khizar– Khizar2012-12-24 10:53:09 +00:00Commented Dec 24, 2012 at 10:53
- I dont know the answer, but looks like question is restricting submit action, upon enter button hit.Naresh– Naresh2012-12-24 10:54:05 +00:00Commented Dec 24, 2012 at 10:54
- 3check whether ur html code is wriitten properlyrOcKiNg RhO– rOcKiNg RhO2012-12-24 10:56:24 +00:00Commented Dec 24, 2012 at 10:56
7 Answers
$('textarea').keypress(function(event) { if (event.which == 13) { event.stopPropagation(); } }); 3 Comments
/n or \n ;)element.addEventListener('keypress', event => ...)'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(); } }); Comments
you can try this code:
$(document).ready(function() { $("textarea").keydown(function(event){ if(event.keyCode == 13) { event.preventDefault(); return false; } }); }); 2 Comments
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.
3 Comments
return false is needed. But with this solution I cannot add a new line anywhere else than in the end of the textarea.$(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.