views:

64

answers:

3

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:

Alternatively, use <input type="text"> instead of <textarea>.

Paul D. Waite
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
+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
+1 for not doing it this way.
ceejayoz