Reputation: 2066
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.
Upvotes: 0
Views: 888
Reputation: 4110
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)">
Upvotes: 0
Reputation: 597106
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.
Upvotes: 5
Reputation: 98796
You’ll need to use JavaScript. Roughly speaking:
onkeypress
event handler on the textarea
submit
function of the formAlternatively, use <input type="text">
instead of <textarea>
.
Upvotes: 1