// On page-load, auto-expand textareas to be tall enough to contain initial content
$('textarea').each(function(){
var pad = parseInt($(this).css('padding-top'));
if ($.browser.mozilla)
$(this).height(1);
var contentHeight = this.scrollHeight;
if (!$.browser.mozilla)
contentHeight -= pad * 2;
if (contentHeight > $(this).height())
$(this).height(contentHeight);
});
//Here is an event to get TextArea expand when you press Enter Key in it.
// intiate a keypress event
$('textarea').keypress(function (e) {
if(e.which == 13) {
var control = e.target;
var controlHeight = $(control).height();
//add some height to existing height of control, I chose 17 as my line-height was 17 for the control
$(control).height(controlHeight+17);
}
});
$('textarea').blur(function (e) {
var textLines = $(this).val().trim().split(/\r*\n/).length;
$(this).val($(this).val().trim()).height(textLines*17);
});
function setTextareaResponsive(id) {
adjustTextareaHeight(id);
$(window).on("resize", function() {
adjustTextareaHeight(id);
});
$("#" + id).on("change input", function() {
adjustTextareaHeight(id);
});
}
function adjustTextareaHeight(id) {
// set height to 1, so scrollHeight is related to linecount, not original textarea size
$("#" + id).height(1);
$("#" + id).height(document.getElementById(id).scrollHeight);
}
function adjustTextareaHeight(id) {
var textarea = document.getElementById(id);
// set height to 1, so scrollHeight is related to linecount, not original textarea size
$("#" + id).height(1);
var lineHeight = parseInt(getComputedStyle(textarea).lineHeight)
var numberOfLines = Math.floor(textarea.scrollHeight / lineHeight)
$("#" + id).height(lineHeight * numberOfLines);
}