Search code examples
javascriptjqueryautosize

Regarding JQuery autosize


I have a textbox that autosizes using JQuery Autosize, however I can't seem to get a certain function to work properly.

When I press the "Enter" key, I need the textbox to go back to 1 row, with no value. However I can't seem to get it to work, my textbox consistently gets 2 rows. I've tried e.preventDefault() to no avail. Can someone help?

I have a fiddle you can access here to look at my code:

http://jsfiddle.net/RZjq7/

Thanks in advance!

Yours, Rei


Solution

  • Rei,

    You need to use preventDefault to cancel the Enter keypress and manual trigger autosize to call the adjust function of the plugin

    $(function() {
        $('textarea').autosize();
        $('textarea').keypress(function(e) {
            if (e.which == 13 && !e.shiftKey) {
                $('textarea').val('');
                $('textarea').trigger('autosize');
                e.preventDefault();
            }
        });
    });​