how can i submit a form on enter pressed while i amm typing in textarea........if i hit enter it only breaks a line but not submits the form.
+1
A:
You’ll need to use JavaScript. Roughly speaking:
- Put an
onkeypress
event handler on thetextarea
- In the event handler, check whether the key that was pressed is enter
- If so, call the
submit
function of the form
Alternatively, use <input type="text">
instead of <textarea>
.
Paul D. Waite
2009-12-21 17:39:08
A:
From http://jennifermadden.com/javascript/stringEnterKeyDetector.html
Use the javascript function:
function checkEnter(e) {
//if which property of event object is supported (NN4)
if(e && e.which) {
//character code is contained in NN4's which property
characterCode = e.which;
}
else {
e = event;
//character code is contained in IE's keyCode property
characterCode = e.keyCode;
}
//if generated character code is equal to ascii 13 (if enter key)
if(characterCode == 13) {
//submit the form
document.forms[0].submit();
return false;
}
else {
return true;
}
}
And then use the following on your textarea:
<textarea onKeyPress="checkEnter(event)">
Brian Hasden
2009-12-21 17:39:36
+5
A:
Try <textarea onkeypress="handleKeyEvent(event);">
function handleKeyEvent(e) {
var charCode;
if (e && e.which) {
charCode = e.which;
} else if (window.event) {
e = window.event;
charCode = e.keyCode;
}
if (charCode == 13) {
document.getElementById("yourForm").submit();
}
}
But I'd advise against such a thing, or at least make sure that users know what will happen when they press enter. Generally they expect a line break.
Bozho
2009-12-21 17:40:01
+1 for not doing it this way.
ceejayoz
2009-12-21 18:02:55