javascript - Force resizement when reading text from file -
the duplicate suggested question got basis question, not duplicate! matter of fact, already have linked question start...
good edit:
i made jsfiddle ( first time :) ). notice how textarea not expand 1 wish. type inside textarea , resized immediately.
if can automatically send keypress event, may work... (this relevant question did not help, answers had no effect).
i using textarea here. then, read file , putting content inside textbox, doesn't resized should.
i update textbox this:
function updatetextbox(text) { $('#cagetextbox').val(text); }; any ideas?
i on firefox 34.0 canonical, @ ubuntu 12.04, if plays role, hope not case, since of users use chrome.
edit:
an attempt write this:
$('#cagetextbox').attr("rows", how many rows new text contain); note if try find out how many lines of text textarea contains, 1 answer.
edit_2
maybe width/(length_of_line * font_size). few tests, seems correct, if subtracted 1 (by keeping integer part of result of course).
your textarea didn't update height when trigger 'input.textarea' on because value undefined:
this.basescrollheight it's defined after 'focus' on textarea.
i have modified code 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 basescrollheight calculation separate function
function calcbasescrollheight(textarea) { var savedvalue = textarea.value, basescrollheight; textarea.value = ''; basescrollheight = textarea.scrollheight; textarea.value = savedvalue; return basescrollheight; } then call updatetextbox this:
$('#cagetextbox').val(text).trigger('input.textarea');
Comments
Post a Comment