From 97cfc7b707d97b0d479fc5fbb7ea79cda3cb25a2 Mon Sep 17 00:00:00 2001 From: Karl Swedberg Date: Thu, 30 Apr 2009 13:16:51 -0400 Subject: [PATCH] added init option to set textarea height initially on load. fixed set/clear Interval. --- jquery.expandable.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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(); } });