Connecting Tech Pros Worldwide Forums | Help | Site Map

Automatically extending textarea

Member
 
Join Date: Oct 2006
Posts: 35
#1: Oct 15 '06
I wish to create a textarea which will extend its height according to how many lines there in it so that one won't need to scroll. To do this I have created a textarea thus:

Expand|Select|Wrap|Line Numbers
  1. <textarea id="myText" cols=10 rows=1 wrap="hard" style="overflow-y:hidden; width:50%; height:25px;" onKeyUp="wordWrap()"></textarea>
  2.  
Everytime the user type in the textarea it calls the function 'wordWrap()' which detects the number of lines in the textarea and adjusts the height accordingling:

Expand|Select|Wrap|Line Numbers
  1. var height = 25;
  2. function wordWrap()
  3.   {
  4.     n = editArea.value.split(/\n/).length;
  5.     height = n*15;
  6.     editArea.style.height = (height+10)+'px';
  7.     window.status = n;
  8.   }
  9.  
The problem I have is that this function will only calculate the line created by the user pressing ENTER and not lines created by automatic (hard) wrap (which to my knowledge inserts carriage returns automatically where the lines break).

How can I detect all newlines/line breaks/carriage returns in my textarea? Has anyone done something like this before. I've scoured the internet for a solution without luck.

Netvibes.com have manage to get this to work almost perfect. But I've noticed a little glitch when u write a word that is too long.

iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#2: Oct 15 '06

re: Automatically extending textarea


hmm have you tried splitting it by line feed instead of carriage return? which would be \n if i am not mistaking
Member
 
Join Date: Oct 2006
Posts: 35
#3: Oct 16 '06

re: Automatically extending textarea


I believe that is what I'm doing with:

n = myText.value.split(/\n/).length;

Or do you mean something else?
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#4: Oct 16 '06

re: Automatically extending textarea


Quote:

Originally Posted by carllucas

I believe that is what I'm doing with:

n = myText.value.split(/\n/).length;

Or do you mean something else?

I can't remember how word wrap works exactly if it adds a line feed and carriage return, drawing blanks right now will try to figure it out and post later with some help.
Newbie
 
Join Date: Mar 2008
Posts: 1
#5: Mar 11 '08

re: Automatically extending textarea


in visual studio I use
<asp:textbox id="txttxtarea" runat="server" TextMode="MultiLine" Height="0px" >txtarea</asp:textbox>
This creates a textarea with auto height
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#6: Mar 12 '08

re: Automatically extending textarea


the wraps inserted by the browser do not affect the content of the textarea, they can't, less the mess up form submitting.

you can increase the rows propery of the textarea instead, and that will prevent the line breaks, and thus give you an accurate estimation of the needed height.
Member
 
Join Date: Nov 2007
Posts: 77
#7: Mar 12 '08

re: Automatically extending textarea


try this, give your textarea a unique id such as [id='txt4']

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script type='text/javascript'>
  3. window.onload = function()
  4. {
  5.   txtOnTimeout();
  6. }
  7. function txtOnTimeout()
  8. {
  9.   var t4 = document.getElementById('txt4');
  10.   if (t4.value.length > t4.rows * t4.cols) {
  11.     ++t4.rows;
  12.   }  
  13.   setTimeout('txtOnTimeout()', 500);
  14. }
  15. </script>
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#8: Mar 12 '08

re: Automatically extending textarea


This works pretty well, but when the textarea gets bigger than the window you will have to scroll the window. And make sure none of the textarea's ancestors has an absolute height style property, or they will get the scroll bar.

Expand|Select|Wrap|Line Numbers
  1. Run.rerow= function(e){
  2.     e= window.event || e;
  3.     var T= e.target || e.srcElement;
  4.     while(T.scrollHeight>T.offsetHeight){
  5.         T.rows= parseInt(T.getAttribute('rows'))+2;
  6.         T.focus();
  7.     }
  8. }
// test
var T= document.getElementsByTagName('textarea')[0];
T.onkeypress= Run.rerow;
Reply