How do you catch a carriage-return in a textarea and do a form post instead of a newline in the textarea?
+1
A:
Add an onKeyPress function to the textarea, and have it intercept Enter (character code 13) and submit the form instead.
Here's an example that uses a text input instead of textarea, but it should work the same way.
<textarea name="myTextArea" onKeyPress="checkEnter(event)"></textarea>
Kaleb Brasee
2009-11-02 18:21:47
+1
A:
The basic skeleton (from the API docs):
$('#textarea-selector-here').keydown(function(event)
{
switch(event.keyCode)
{
// ...
// different keys do different things
// Different browsers provide different codes
// see here for details: http://unixpapa.com/js/key.html
// ...
}
});
However, if you don't want to allow multiline input, why not just use an <input type="text" />
?
Matt Ball
2009-11-02 18:22:57
Surely you mean type="text" :)
Kevin Peno
2009-11-02 18:28:15
Yup, sorry for the typo.
Matt Ball
2009-11-02 19:19:34
You may have a point TStamper because I'm having trouble with the $(this).parents('form').submit();not working.
cf_PhillipSenn
2009-11-02 19:29:19
@psenn- I may have a point regarding what?
TStamper
2009-11-02 20:10:38
+4
A:
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.
CMS
2009-11-02 18:28:25