473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Automatically extending textarea

35 New Member
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.
Oct 15 '06 #1
7 4566
iam_clint
1,208 Recognized Expert Top Contributor
hmm have you tried splitting it by line feed instead of carriage return? which would be \n if i am not mistaking
Oct 15 '06 #2
carllucas
35 New Member
I believe that is what I'm doing with:

n = myText.value.sp lit(/\n/).length;

Or do you mean something else?
Oct 16 '06 #3
iam_clint
1,208 Recognized Expert Top Contributor
I believe that is what I'm doing with:

n = myText.value.sp lit(/\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.
Oct 16 '06 #4
eilering
1 New Member
in visual studio I use
<asp:textbox id="txttxtarea " runat="server" TextMode="Multi Line" Height="0px" >txtarea</asp:textbox>
This creates a textarea with auto height
Mar 11 '08 #5
rnd me
427 Recognized Expert Contributor
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.
Mar 11 '08 #6
Inny
77 New Member
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>
Mar 12 '08 #7
mrhoo
428 Contributor
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.getEle mentsByTagName( 'textarea')[0];
T.onkeypress= Run.rerow;
Mar 12 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
11445
by: jason | last post by:
Hello. I've written this web application in asp.net and have a textarea what I will use like a chat box. The box will be frequently refreshing. I need the textarea to *automatically* scroll to the bottom or start at the bottom displaying the last message sent. I am being told there is no way to do this in asp.net and that I will have to do it in javascript. Does anybody have any sample code on how to do this? Many thanks for any...
4
16725
by: Csaba Gabor | last post by:
What I'd like to do is to be able to set the font of a textarea element to the same font that another element is using (say, for example, an <INPUT type=text ...> element, but if that's a no go, then a generic element's font will do OK, too. What's the correct way to do this, please (so that it will also work for IE 6)? The motivation for this is that I have some text on the screen and I want to insert a textarea element between the...
1
3576
by: Volt | last post by:
is there any way to select and mark part of text in textarea by regular expression? i need to select the first string in textarea whitch is like xxxxx,xxx where x is any character
4
4263
by: TJS | last post by:
can the rows and columns of a textarea element with an id be changed on the fly ? if so is there an example ?
3
5557
by: arashamiri | last post by:
Hi, I wrote an auto resizing textarea: function myMax(anumber, another) { if (anumber > another) { return anumber; } return another; }
0
2187
by: Robert Lummert | last post by:
Hi there, I've got a severe problem with pear when trying to install pdflib. First pear demanded a program named phpize. After searching a while I found that phpize is part of php-devel while pear is part of the php package itself. Maybe that should be changed as it makes absolutely no sense having pear without phpize. Now after installing php-devel pear still fails, now stating:
6
5992
by: wperry1 | last post by:
I am writing a small database utility to catalog all of my favorite ASM/JS/VBS... functions and scripts on an asp page. Everything is going smoothly except for one thing that I can't quite seem to think my way around. I am using a <textarea> to display the code but some of my scripts have <textarea> tags in them and when the </textarea> tag is entered, it closes my <textarea> and the rest of the code ends up outside of the textarea.
3
2748
by: Stephen V | last post by:
Can anyone help me out with this problem. I would like to redirect/ go to another php page when my SQL insertion is done and also send a variable at the same time. the idea is, to come on my page for the first time and not doing anything, then I fill up my form and insert the data (with the button "add request" and when the oci_execute is done go to another page. all I need to know is how to go to another page automatically... and...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10327
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.