Maybe like this, also adding a linecounter so adding or removing newlines makes it add or remove more than if it's only a normal character. function textarea_change() { // https://stackoverflow.com/questions/7745741/auto-expanding-textarea for (const textarea of document.getElementsByTagName("textarea")) { textarea.onfocus = "" textarea.onblur = "" textarea.setAttribute("charcount", textarea.value.length); textarea.setAttribute("linecount", textarea.value.split(/\r\n|\r|\n/).length); textarea.style.height = Math.max(80, textarea.scrollHeight) + "px"; textarea.oninput = function() { var textlen = textarea.value.length, charcount = textarea.getAttribute("charcount"), chardiff = charcount - textlen, linelen = textarea.value.split(/\r\n|\r|\n/).length, linecount = textarea.getAttribute("linecount"), linediff = linecount - linelen, charsize = 0.3, linesize = 16, minimum = 80; // reset if zero if (textlen == 0) { textarea.style.height = minimum + "px"; return; } if (linelen != linecount) { textarea.style.height = Math.max(minimum, parseFloat(textarea.style.height).toFixed(2) - (linesize*linediff) ) + "px"; } if (textlen != charcount) { textarea.style.height = Math.max(minimum, parseFloat(textarea.style.height).toFixed(2) - (charsize*chardiff)) + "px"; } textarea.setAttribute("linecount", linelen); textarea.setAttribute("charcount", textlen); } if (autofill_sage) textarea_autofill_sage(textarea) } }