I have a textarea that when a users pressed enter/return on their keyboard, I want to submit the field to a JavaScript function, just like it works on Twitter.
Ideas?
Thanks
I have a textarea that when a users pressed enter/return on their keyboard, I want to submit the field to a JavaScript function, just like it works on Twitter.
Ideas?
Thanks
Subscribe for the onkeydown
event and detect the return key code.
Just use a normal text input, this functionality comes built-in. Textareas are supposed to be "returnable", making the form submit on return in a textarea will confuse your users.
If you're using jQuery, you could do this:
$('#myTextarea').keydown(function(e) {
if(e.which == 13) {
// Enter was pressed. Run your code.
}
});
The keypress
handler runs on each key press, and tests for the 'enter' key.
EDIT:
Changed keypress
to keydown
, as keypress
may run the code multiple times if the user holds down Enter
. Probably not what is desired.