I'm trying to resize a textarea to fit the content in it, as tightly as possible. Here's my current effort:
function resizeTextarea(t) {
a = t.value.split('\n');
b = 1;
for (x = 0; x < a.length; x++) {
c = a[x].length;
if (c >= 75) b += Math.ceiling(c/75);
}
b += a.length;
t.rows = b;
}
This works fairly well, but seems to fail when the user "pushes" text onto the next line by filling up the width. (Note: the 75 used here is representative of the width of my textarea in characters)
There's also an odd effect where [enter][key] makes the textarea 2 lines past the end of the text, then the next [key] takes it back to the expected one extra line. I've tried just setting c to 2 if c<=1, with no effect. This one isn't a huge deal, but it would be nice to correct.
Any help would be appreciated.
Note: this function is called on key down.