Skip to content

Commit

Permalink
added init option to set textarea height initially on load. fixed set…
Browse files Browse the repository at this point in the history
…/clear Interval.
  • Loading branch information
Karl Swedberg committed Apr 30, 2009
1 parent 77b5be0 commit 97cfc7b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions jquery.expandable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$.fn.extend({
expandable: function(options) {
options = $.extend({ duration: 'normal', interval: 750, within: 1, by: 2 }, options);
options = $.extend({ duration: 'normal', interval: 750, within: 1, by: 2, init: false }, options);
return this.filter('textarea').each(function() {
var $this = $(this).css({ display: 'block', overflow: 'hidden' }), minHeight = $this.height(), interval, heightDiff = this.offsetHeight - minHeight,
rowSize = ( parseInt($this.css('lineHeight'), 10) || parseInt($this.css('fontSize'), 10) ),
Expand All @@ -19,8 +19,9 @@ $.fn.extend({
.bind('keypress', function(event) { if ( event.keyCode == '13' ) check(); })
.bind('focus blur', function(event) {
if ( event.type == 'blur' ) clearInterval( interval );
if ( event.type == 'focus' && !interval ) setInterval(check, options.interval);
if ( event.type == 'focus' ) interval = setInterval(check, options.interval);
});

function check() {
var text = $this.val(), newHeight, height, usedHeight, usedRows, availableRows;
$div.html( text.replace(/\n/g, '&nbsp;<br>') );
Expand All @@ -32,10 +33,11 @@ $.fn.extend({
newHeight = rowSize * (usedRows + Math.max(availableRows, 0) + options.by);
$this.stop().animate({ height: newHeight }, options.duration);
} else if ( availableRows > options.by + options.within ) {
newHeight = Math.max( height - (rowSize * (availableRows - (options.by + options.within))), minHeight )
newHeight = Math.max( height - (rowSize * (availableRows - (options.by + options.within))), minHeight );
$this.stop().animate({ height: newHeight }, options.duration);
}
};
if ( options.init ) check();
}).end();
}
});
Expand Down

0 comments on commit 97cfc7b

Please sign in to comment.