Skip to content

Instantly share code, notes, and snippets.

@arvi
Last active June 14, 2016 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arvi/b54c24f10fa792b8eca4595d01476d66 to your computer and use it in GitHub Desktop.
Save arvi/b54c24f10fa792b8eca4595d01476d66 to your computer and use it in GitHub Desktop.
Textarea autoexpand
//autoexpand textarea when initial has input already
$(editBlock).find('textarea').each(function() {
var $this = $(this);
textCount = $this.val().length;
if(textCount > 25) {
rowBreaks = textCount / 25;
rowBreaks = Math.ceil(rowBreaks);
textareaName = $this.attr('name');
$(editBlock).find('textarea[name=' + textareaName + ']').attr({'rows': rowBreaks, 'data-min-rows': rowBreaks});
}
});
//autoexpand textarea: modified from original source: http://codepen.io/vsync/pen/frudD
$('.mockup-survey-builder')
.on('focus.textarea', '.auto-expand', function(){
var savedValue = this.value;
this.value = '';
this.baseScrollHeight = this.scrollHeight;
this.value = savedValue;
})
.on('input.textarea', '.auto-expand', function(){
var minRows = this.getAttribute('data-min-rows')|0,
rows;
this.rows = minRows;
rows = Math.ceil((this.scrollHeight - this.baseScrollHeight) / 30);
this.rows = minRows + rows;
this.rows = (this.rows > 0) ? this.rows : 1;
console.log(this.rows);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment