diff --git a/jquery.expandable.js b/jquery.expandable.js index 525e44d..60fb0b9 100644 --- a/jquery.expandable.js +++ b/jquery.expandable.js @@ -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) ), @@ -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, ' 
') ); @@ -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(); } });