Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shadow does not get built until the the textarea has been rendered. This #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions javascripts/jquery.autogrow-textarea.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

(function($) {

/*
Expand All @@ -6,24 +7,35 @@
$.fn.autogrow = function(options) {

this.filter('textarea').each(function() {

var $this = $(this),
minHeight = $this.height(),
lineHeight = $this.css('lineHeight');

var shadow = $('<div></div>').css({
position: 'absolute',
top: -10000,
left: -10000,
width: $(this).width() - parseInt($this.css('paddingLeft')) - parseInt($this.css('paddingRight')),
fontSize: $this.css('fontSize'),
fontFamily: $this.css('fontFamily'),
lineHeight: $this.css('lineHeight'),
resize: 'none'
}).appendTo(document.body);
minHeight = -1,
lineHeight = '';

var shadow = undefined;

var update = function() {

//only set the shadow if it has not been set already and this is
//the first time that update has been called with $this having a
//height of > 0
if(shadow === undefined && $this.height() !== 0){
minHeight = $this.height(),
lineHeight = $this.css('lineHeight');
shadow = $('<div></div>').css({
position: 'absolute',
top: -10000,
left: -10000,
width: $(this).width() - parseInt($this.css('paddingLeft')) - parseInt($this.css('paddingRight')),
fontSize: $this.css('fontSize'),
fontFamily: $this.css('fontFamily'),
lineHeight: $this.css('lineHeight'),
resize: 'none'
}).appendTo(document.body);
}
//If we do not have a shadow yet, it is because $this does not have
//a height or width. Do nothing.
if(shadow === undefined){
return;
}
var times = function(string, number) {
for (var i = 0, r = ''; i < number; i ++) r += string;
return r;
Expand All @@ -40,15 +52,12 @@
$(this).css('height', Math.max(shadow.height() + 20, minHeight));

}

$(this).change(update).keyup(update).keydown(update);

update.apply(this);

});

return this;

}

})(jQuery);
})(jQuery);