473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to automatically expand textarea

ifedi
60
I need to make a textarea grow automatically (in height) as the user entry flows over to a new line...
Actually I got this working well, only when ENTER is hit (I made use of the total number of \r\n in the textarea content, and setting the textarea ROWS attribute to this), but am at a loss implementing this for the situation when the text WRAPS by itself (ie with long text lines). The problem is that a long phrase may actually occupy 3 lines visually, but is counted as only one \r\n, leading to partially hidden text and overflow scroll situation.
Any help is highly welcome
Jul 9 '10 #1
4 2627
improvcornartist
303 Expert 100+
You could get the textarea value length and cols value to determine how many rows you want and apply it on key up/down or however fits best.
Expand|Select|Wrap|Line Numbers
  1. textarea.rows = Math.ceil(textarea.value.length / textarea.cols);
Jul 9 '10 #2
ifedi
60
@improvcornartist
Thanks improvcornartist for your kind reply.
However, I have like 2 issues with this solution:
Expand|Select|Wrap|Line Numbers
  1. textarea.rows = Math.ceil(textarea.value.length / textarea.cols); 
  2.  
1. It seems I would therefore need to supply the COLS attribute for any textarea needing this functionality. Problem is I've mostly used the STYLE.WIDTH instead, to determine the textarea widths.
2. Even given the COLS attribute, the code rather fails when the textarea contains CRLF on lines that do not contain the COLS number of characters, like so:

Lorem ipsum dolor sit amet consectetuer Lorem pretium molestie congue Curabitur. Et at augue at morbi at adipiscing condimentum wisi lorem In.
1.Lorem
2.ipsum
3.dolor
4.sit
5.condimentum

With this example, the the textarea does not expand as frequently as expected, because each of the shorter lines does not meet the COLS attribute, to cause a lengthening!
I remain grateful for your further attention to this.
Regards, Ifedi.
Jul 14 '10 #3
improvcornartist
303 Expert 100+
You are right, that is a very generic approach. You will probably need to try a combination of things, such as considering the text length as well as the number of newlines. You may be able to use something like textarea.scrollHeight.
Jul 14 '10 #4
ifedi
60
@improvcornartist
Yes!!
As a matter of fact, it was not "a combination of newlines and text length" that did it; it was textarea.scrollHeight.
Bravo brother, I appreciate your insight.
Ifedi.
Jul 20 '10 #5

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

Similar topics

5
by: Matt | last post by:
Simple vbscript,html page loading into access. Input pg posting to Confirm pg, then sending to access. when using; <textarea name="name" tabindex="15"> </textarea> as an input renders tab...
8
by: 2centbob | last post by:
Has anyone had an issue with SQL Server not being able to expand against a RAID 5 file system? My current configuration is that the server is started and stopped using the local system account. I...
4
by: Steve | last post by:
Hi, I'm opening a small window with window.open. I have a textarea there, and I want it to expand to fit the window as it's resized. How is this possible? Thanks, Steve
1
by: Steven | last post by:
Hello, I have a problem in using TreeView. I want to know how the TreeView can automatically Expand a path I have accessed before? I have tried to use "path.Expand()", but it seems no good...
3
by: arashamiri | last post by:
Hi, I wrote an auto resizing textarea: function myMax(anumber, another) { if (anumber > another) { return anumber; } return another; }
4
by: Werner | last post by:
Hello, how can fill automatically a textarea when opening a webpage. I want to do this by a function that has been raised in js file. thx
7
by: carllucas | last post by:
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: <textarea...
8
by: Lynx101 | last post by:
Hi, hope anyone can help with this query. Background: Subform datasheet link to a form to store updates on projects. Query: I have a datasheet where one of the field is a memo field. When...
7
by: dorandoran | last post by:
I used this article (http://www.codeproject.com/KB/webforms/EditNestedGridView.aspx) to accomplish expand/collaspe grid. I also added a search and it works. but i need to be now automatically expand...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.