Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  127] [ 4]  / answers: 1 / hits: 55638  / 14 Years ago, mon, december 13, 2010, 12:00:00

I have a textarea like this:



<textarea tabindex=1 maxlength='2000' id=area></textarea>



I watch this textarea with jquery:



$(#area).keypress(function (e) {
if (e.keyCode != 13) return;
var msg = $(#area).val().replace(n, );
if (!util.isBlank(msg))
{
send(msg);
$(#area).val();
}
});


send() submits the message to the server if the return key was pressed and if the message is not blank or only containing line spaces.



The problem: After sending the message, the textarea is not cleared.
On the first page load, the textarea is empty. Once a message was submitted, there is one blank line in the textarea and I don't know how to get rid of it.


More From » jquery

 Answers
16

The problem is that the Enter keypress is not being suppressed and is doing its usual browser behaviour (i.e. adding a line break). Add return false to the end of your keypress handler to prevent this.



$(#area).keypress(function (e) {
if (e.keyCode != 13) return;
var msg = $(#area).val().replace(/n/g, );
if (!util.isBlank(msg))
{
send(msg);
$(#area).val();
}
return false;
});

[#94620] Friday, December 10, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;