forked from akaihola/jquery-simpleautogrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simpleautogrow.js
42 lines (37 loc) · 1.46 KB
/
jquery.simpleautogrow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Simple Auto Expanding Text Area (0.1.2-dev)
* by Antti Kaihola (antti.kaihola.fi)
*
* Copyright (c) 2009 Antti Kaihola (antti.kaihola.fi)
* Licensed under the MIT and BSD licenses.
*
* NOTE: This script requires jQuery to work. Download jQuery at
* www.jquery.com
*/
(function(jQuery) {
jQuery.fn.simpleautogrow = function(margin) {
return this.each(function() { new jQuery.simpleautogrow(this, margin); }); };
jQuery.simpleautogrow = function (e, margin) {
var self = this;
var $e = this.textarea = jQuery(e)
.css({overflow: 'hidden', display: 'block'})
.bind('focus', function() {
this.timer = window.setInterval(function() {self.checkExpand(); }, 200); })
.bind('blur', function() { clearInterval(this.timer); });
this.border = $e.outerHeight() - $e.innerHeight();
try { margin = typeof margin == 'undefined' ? 0 : parseInt(margin); } catch(ex) { margin = 0; }
this.margin = margin;
this.clone = $e.clone().css({position: 'absolute', visibility: 'hidden'})
.attr('name', '').attr('id', '');
$e.height(e.scrollHeight + this.border)
.after(this.clone);
this.checkExpand();
};
jQuery.simpleautogrow.prototype.checkExpand = function() {
var target_height = this.clone[0].scrollHeight + this.border + this.margin;
if (this.textarea.outerHeight() != target_height)
this.textarea.height(target_height + 'px');
this.clone.attr('value', this.textarea.attr('value')).height(0);
};
})(jQuery);