ClanKiller.com
http://forums.clankiller.com/

javascript resize textarea
http://forums.clankiller.com/viewtopic.php?f=24&t=2927
Page 1 of 1

Author:  Satis [ Mon Sep 29, 2008 1:24 pm ]
Post subject:  javascript resize textarea

I found this online here:
http://tuckey.org/textareasizer/

Basically I was looking for something that would automatically resize a textbox to match the text inside of it. The application was for a web app where the text could be of virtually any size and it had to be user-editable through the web page.

I took the javascript referenced above and made some minor changes.

inline on the textarea
Code:
"<textarea cols=80 onkeyup='resizeTextarea(this);'>" + issueDescription + "</textarea>"


then the javascript functions
Code:
function countLines(strtocount, cols) {
   var hard_lines = 1;
   var last = 0;
   while ( true ) {
      last = strtocount.indexOf("\n", last+1);
      hard_lines ++;
      if ( last == -1 ) break;
   }
   var soft_lines = Math.round(strtocount.length / (cols-1));
   var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
   if ( hard ) soft_lines = hard_lines;
   return soft_lines;
}
function resizeTextarea(el){
   el.rows = countLines(el.value, el.cols+1);
}


The biggest drawback is that you have to define a cols property on the textarea. I'd recently been using style='width: 100%;' to just make it automatically resize to the area it's in, but that doesn't work here. Still, it's very handy.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/