tags:

views:

37

answers:

2

Is there anyway I can submit a form without a submit button, but only when the user press the Enter button? Kind of like when commenting on Facebook, you have the TEXTAREA but no submit button. Once you press enter the form submitted with Ajax.

Thanks,

Joel

A: 

The function below checks if the keycode is 13, or the Enter key. If it is, the function calls the submitformnow function.

<script type="text/javascript">
function submitform(myfield, e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{   
    submitformnow();
    return false;
}
else return true;
}
function submitformnow(){document.myform.submit();}
</script>

Then, in your textarea, place onkeypress="return submitform(this, event)".

Evan Mulawski
+2  A: 

Set an onkeyup or onkeydown event on your textarea, the latter is better because it fires before the new line is added to the textarea

var $form = $( '#yourForm' );

$( '#yourTextArea' ).keydown(function( e ){
    if( e.keyCode == 13 ){
        $form.submit();
    }
});

$form.submit(function(){
    alert( 'submitted' );
    return false;
});
Harmen