Last active
June 14, 2016 10:47
-
-
Save arvi/b54c24f10fa792b8eca4595d01476d66 to your computer and use it in GitHub Desktop.
Textarea autoexpand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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