source: trunk/template-common/lib/plugins/jquery.autogrow-textarea.js @ 4067

Last change on this file since 4067 was 3282, checked in by plg, 15 years ago

change: according to topic:15067, svn:keywords property was removed

  • Property svn:eol-style set to LF
File size: 1.4 KB
Line 
1(function($) {
2
3    /*
4     * Auto-growing textareas; technique ripped from Facebook
5     */
6    $.fn.autogrow = function(options) {
7       
8        this.filter('textarea').each(function() {
9           
10            var $this       = $(this),
11                minHeight   = $this.height(),
12                lineHeight  = $this.css('lineHeight');
13           
14            var shadow = $('<div></div>').css({
15                position:   'absolute',
16                top:        -10000,
17                left:       -10000,
18                width:      $(this).width(),
19                fontSize:   $this.css('fontSize'),
20                fontFamily: $this.css('fontFamily'),
21                lineHeight: $this.css('lineHeight'),
22                resize:     'none'
23            }).appendTo(document.body);
24           
25            var update = function() {
26               
27                var val = this.value.replace(/</g, '&lt;')
28                                    .replace(/>/g, '&gt;')
29                                    .replace(/&/g, '&amp;')
30                                    .replace(/\n/g, '<br/>');
31               
32                shadow.html(val);
33                $(this).css('height', Math.max(shadow.height() + 20, minHeight));
34           
35            }
36           
37            $(this).change(update).keyup(update).keydown(update);
38           
39            update.apply(this);
40           
41        });
42       
43        return this;
44       
45    }
46   
47})(jQuery);
Note: See TracBrowser for help on using the repository browser.