Force Resizement When Reading Text From File
Solution 1:
Your textArea didn't update the height itself even when you trigger the 'input.textarea' on it because this value is undefined:
this.baseScrollHeight
It's only defined after a 'focus' on the textarea.
I have modified your code a little bit: http://jsfiddle.net/n1c41ejb/4/
so, on 'input'
$(document).on('input.textarea', '.autoExpand', function(){
var minRows = this.getAttribute('data-min-rows')|0,
rows;
this.rows = minRows;
this.baseScrollHeight = this.baseScrollHeight | calcBaseScrollHeight(this);
rows = Math.ceil((this.scrollHeight - this.baseScrollHeight) / 16);
this.rows = minRows + rows;
});
and move the baseScrollHeight calculation to a separate function
function calcBaseScrollHeight(textArea) {
var savedValue = textArea.value,
baseScrollHeight;
textArea.value = '';
baseScrollHeight = textArea.scrollHeight;
textArea.value = savedValue;
return baseScrollHeight;
}
then call your updateTextBox like this:
$('#cagetextbox').val(text).trigger('input.textarea');
Solution 2:
You can use the following code to adjust the height of an element, in your case a textarea. While it does use a loop, it avoids several display and scrolling problems that way.
function autoheight(a) {
if (!$(a).prop('scrollTop')) {
do {
var b = $(a).prop('scrollHeight');
var h = $(a).height();
$(a).height(h - 5);
}
while (b && (b != $(a).prop('scrollHeight')));
};
$(a).height($(a).prop('scrollHeight'));
}
Resize on any events you like.
$("#cagetextbox").on("keyup change", function (e) {
autoheight(this);
});
And/or when events are not triggered automatically call it yourself.
function updateTextbox(text) {
$('#cagetextbox').val(text);
autoheight($("#cagetextbox"));
};
Solution 3:
I created a working fiddle. It creates an input textfield exactly as if it were displayed in the non editable div.
https://jsfiddle.net/khgk7nt5/2/
Example CSS
.test{
font-family:"Times New Roman", Times, serif;
font-color:#CCCCCC;
font-size:20px;
width:300px;
}
.testLoc{
position:absolute;
top:-1000px;
}
JavaScript
var testText = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore"
var test = $("<div/>");
test.addClass("test testLoc");
test.text(testText);
$("body").append(test);
var out = $("<textarea/>");
out.addClass("test");
out.width(test.width());
out.height(test.height());
out.val(testText);
$("body").append(out);
test.remove();
Post a Comment for "Force Resizement When Reading Text From File"