For TEXTAREA, how to call a JavaScript function on return / input

I have a textbox that when users hit enter / return on their keyboard, I want to send the field to a JavaScript function, just like it does on Twitter.

Ideas?

thanks

+2


a source to share


3 answers


If you are using jQuery you can do this:

$('#myTextarea').keydown(function(e) {
    if(e.which == 13) {
        // Enter was pressed. Run your code.
    }
});

      

The handler keypress

runs on every key press and checks the 'enter' key.



EDIT:

Changed keypress

to keydown

as it keypress

can run code multiple times if the user holds Enter

. Probably not what you need.

+2


a source


Just use normal text input, this functionality is built in. Text boxes are supposed to "return" making the submit form on return in a text box will confuse your users.



+2


a source


Subscribe to the event onkeydown

and define the return key code.

+1


a source







All Articles