-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.simpleautogrow.js
37 lines (32 loc) · 1.35 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
/*
* 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() {
return this.each(function() { new jQuery.simpleautogrow(this); }); };
jQuery.simpleautogrow = function (e) {
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();
this.clone = $e.clone().css({position: 'absolute', visibility: 'hidden', width: ($e.innerWidth()+'px')}).attr('name', '')
$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;
if (this.textarea.outerHeight() != target_height)
this.textarea.height(target_height + 'px');
this.clone.attr('value', this.textarea.attr('value')).height(0); };
})(jQuery);