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

jquery.autogrow updates #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 9 additions & 13 deletions javascripts/jquery.autogrow-textarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
* Auto-growing textareas; technique ripped from Facebook
*
* http://github.com/jaz303/jquery-grab-bag/tree/master/javascripts/jquery.autogrow-textarea.js
*
* Requires jQuery 1.4+
*/
$.fn.autogrow = function(options)
$.fn.autogrow = function()
{
return this.filter('textarea').each(function()
{
var self = this;
var $self = $(self);
var minHeight = $self.height();
var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight')) || 0;
var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight'),10) || 0;

var shadow = $('<div></div>').css({
var shadow = $('<div />').css({
position: 'absolute',
top: -10000,
left: -10000,
Expand All @@ -24,23 +26,17 @@
fontWeight: $self.css('fontWeight'),
lineHeight: $self.css('lineHeight'),
resize: 'none',
'word-wrap': 'break-word'
'word-wrap': 'break-word'
}).appendTo(document.body);

var update = function(event)
{
var times = function(string, number)
{
for (var i=0, r=''; i<number; i++) r += string;
return r;
};

var val = self.value.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/&/g, '&amp;')
.replace(/\n$/, '<br/>&nbsp;')
.replace(/\n/g, '<br/>')
.replace(/ {2,}/g, function(space){ return times('&nbsp;', space.length - 1) + ' ' });
.replace(/ {2,}/g, function(space){ return new Array(space.length).join('&nbsp;') + ' '; });

// Did enter get pressed? Resize in this keydown event so that the flicker doesn't occur.
if (event && event.data && event.data.event === 'keydown' && event.keyCode === 13) {
Expand All @@ -50,9 +46,9 @@
shadow.css('width', $self.width());
shadow.html(val + (noFlickerPad === 0 ? '...' : '')); // Append '...' to resize pre-emptively.
$self.height(Math.max(shadow.height() + noFlickerPad, minHeight));
}
};

$self.change(update).keyup(update).keydown({event:'keydown'},update);
$self.change(update).keyup(update).keydown({event:'keydown'},update).bind('input paste',update);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for adding bind('input paste')

$(window).resize(update);

update();
Expand Down
1 change: 1 addition & 0 deletions javascripts/jquery.autogrow-textarea.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.