It is currently Mon Apr 23, 2018 11:28 am

Reply to topic  [ 1 post ] 
javascript resize textarea 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 15743
Location: On a slope
Reply with quote
Post javascript resize textarea
I found this online here:

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
"<textarea cols=80 onkeyup='resizeTextarea(this);'>" + issueDescription + "</textarea>"

then the javascript functions
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.

They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.

Mon Sep 29, 2008 1:24 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.