I am using the accepted answer to this question to build a textarea that expands vertically as text overflows:
A way to do the accepted answer when the textarea is in a scrollable div:
function getScrollParent(node) {
if (node == null) {
return null;
}
if (node.scrollHeight > node.clientHeight) {
return node;
} else {
return getScrollParent(node.parentNode);
}
}
function resize(){
// 'this' is the textarea
const scrollParent = getScrollParent(this);
const scrollTop = scrollParent ? scrollParent.scrollTop : null;
const scrollLeft = scrollParent ? scrollParent.scrollLeft : null;
this.style.height = "auto";
this.style.height = this.scrollHeight + "px";
if (scrollParent) {
scrollParent.scrollTo(scrollLeft, scrollTop);
}
};