Resize Textarea On Load
I'm having issues trying to resize a text area, I can do it as the user is typing but when they have submit it this gets put into a database and put into a text area below and disp
Solution 1:
See my modification of prior solution: http://jsfiddle.net/CbqFv/570/
HTML
<textarea cols="42" rows="1">
1 ...by default, you can write more rows</textarea><br />
<br /><textareacols="42"rows="5">
1
2
3
4
5 ...by default, you can write more rows</textarea><br /><br /><textareacols="42"rows="10">
1
2
3
4
5
6
7
8
9
10 ...by default, you can write more rows</textarea>
CSS
textarea {
border: 1px solid gray;
border-radius: 3px;
line-height: 1.3em;
overflow: hidden;
padding: 0.3em0.3em00.3em;
outline: none;
background-color: white;
resize: none;
}
JavaScript
var observe;
if (window.attachEvent) {
observe = function (element, event, handler) {
element.attachEvent('on'+event, handler);
};
}
else {
observe = function (element, event, handler) {
element.addEventListener(event, handler, false);
};
}
functioninit () {
functionresize (element) {
element.style.height = 'auto';
element.style.height = element.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */functiondelayedResize (element) {
window.setTimeout(function() { resize(element) }, 0);
}
var textareas = document.getElementsByTagName("textarea");
for (i = 0; i < textareas.length; i++) {
var textarea = textareas[i];
observe(textarea, 'change', function() { resize(this) });
observe(textarea, 'cut', function() { delayedResize(this) });
observe(textarea, 'paste', function() { delayedResize(this) });
observe(textarea, 'drop',function() { delayedResize(this) });
observe(textarea, 'keydown', function() { delayedResize(this) });
textarea.focus();
textarea.select();
resize(textarea);
}
}
init();
Solution 2:
from autosize documentation:
Autosize has no way of knowing when the value of a textarea has been changed through JavaScript. If you do this, trigger the autosize.resize event immediately after to update the height. Example:
$('#example').val('New Text!').trigger('autosize.resize');
So after you update the textarea content, you'll have to call that trigger to autoresize it
Post a Comment for "Resize Textarea On Load"