A COMPLETE YET SIMPLE SOLUTION
The following code will work:
- On key input.
- With pasted text (right click & ctrl+v).
- With cut text (right click & ctrl+x).
- With pre-loaded text.
- With all textareas (multiline textboxes) site wide.
- With Firefox (v31-109 tested).
- With Chrome (v37-108 tested).
- With IE (v9-v11 tested).
- With Edge (v14-v108 tested).
- With IOS Safari.
- With Android Browser.
- With JavaScript strict mode.
OPTION 1 (With jQuery)
This option requires jQuery and has been tested and is working with 1.7.2 - 3.7.1
Simple (Add this jQuery code to your master script file and forget about it.)
$("textarea").each(function () {
this.style.height = this.scrollHeight + "px";
this.style.overflowY = "hidden";
}).on("input", function () {
this.style.height = "auto";
this.style.height = this.scrollHeight + "px";
});
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.7.1.min.js"></script>
<textarea placeholder="Type, paste, cut text here...">PRELOADED TEXT.
This JavaScript should now add better support for IOS browsers and Android browsers.</textarea>
<textarea placeholder="Type, paste, cut text here..."></textarea>
Test on jsfiddle
OPTION 2 (Pure JavaScript)
Simple (Add this JavaScript to your master script file and forget about it.)
document.querySelectorAll("textarea").forEach(function(textarea) {
textarea.style.height = textarea.scrollHeight + "px";
textarea.style.overflowY = "hidden";
textarea.addEventListener("input", function() {
this.style.height = "auto";
this.style.height = this.scrollHeight + "px";
});
});
<textarea placeholder="Type, paste, cut text here...">PRELOADED TEXT. This JavaScript should now add better support for IOS browsers and Android browsers.</textarea>
<textarea placeholder="Type, paste, cut text here..."></textarea>
Test on jsfiddle
OPTION 3 (jQuery Extension)
Useful if you want to apply further chaining to the textareas, you want to be auto-sized.
jQuery.fn.extend({
autoHeight: function () {
function setAutoHeight(element) {
jQuery(element).css({ height: 'auto', overflowY: 'hidden' });
jQuery(element).height(element.scrollHeight);
}
return this.each(function() {
setAutoHeight(this);
jQuery(this).on("input", () => setAutoHeight(this));
});
}
});
Invoke with $("textarea").autoHeight()
UPDATING TEXTAREA VIA JAVASCRIPT
When injecting content into a textarea via JavaScript, append the following line of code to invoke the resize function.
jQuery
$("textarea").trigger("input");
Pure JavaScript
document.querySelectorAll("textarea").forEach(t => t.dispatchEvent(new Event('input', { bubbles: true, cancelable: true })));
PRESET TEXTAREA HEIGHT
To fix the initial height of the textarea you will need to add another condition:
const txHeight = 16; // Preset initial height in pixels
const tx = document.getElementsByTagName("textarea");
for (let i = 0; i < tx.length; i++) {
if (tx[i].value === '') {
tx[i].style.height = txHeight + "px";
} else {
tx[i].style.height = tx[i].scrollHeight + "px";
}
tx[i].style.overflowY = "hidden";
tx[i].addEventListener("input", OnInput, false);
}
function OnInput() {
this.style.height = 'auto';
this.style.height = this.scrollHeight + "px";
}
<textarea placeholder="Type, paste, cut text here...">PRELOADED TEXT. This JavaScript should now add better support for IOS browsers and Android browsers.</textarea>
<textarea placeholder="Type, paste, cut text here..."></textarea>
function textAreaAdjust(o) { o.style.height = "1px"; o.style.height = (25+o.scrollHeight)+"px"; }<textarea onkeyup="textAreaAdjust(this)" style="overflow:hidden"></textarea>