473,406 Members | 2,371 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,406 software developers and data experts.

dynamically resizing height of textarea of known width

Hi,

I am setting the width of a textarea using the style.width to ensure
the textarea is exactly the right width.

I want to set the height/rows of the textarea depending on it's
contents (so that it dynamically grows and shrinks as the contents
alter) however I can not see a way to calculate the required number of
rows (or height) for a given content string.

If all the lines of content are shorter that than the width of the
area then it's simple but obviously this is not always the case, so I
have a function that splits the content into lines then each line into
single words and iterates thro adding words to a tmp string whose
width (not length) i then want to compare with the width of the
textarea however I cant see how I can do that.

I tried to use the number of cols and the length of the string but
the number of cols is not set when the style.width is set.

Anyone have any advice on how to go about this?

G

Mar 12 '07 #1
6 26974
Ivo
<gi************@whoever.comtyped
I am setting the width of a textarea using the style.width to ensure
the textarea is exactly the right width.

I want to set the height/rows of the textarea depending on it's
contents (so that it dynamically grows and shrinks as the contents
alter) however I can not see a way to calculate the required number of
rows (or height) for a given content string.

If all the lines of content are shorter that than the width of the
area then it's simple but obviously this is not always the case, so I
have a function that splits the content into lines then each line into
<snip>

Instead of measuring the text, compare the offsetHeight and scrollHeight of
the textarea. If the scrollHeight is the larger, you know you need to
increase the area's size.
hth
ivo
http://4umi.com/web/javascript/
Mar 12 '07 #2
On 12 Mar, 17:06, "Ivo" <n...@thank.youwrote:
<gilbert.geo...@whoever.comtypedI am setting the width of a textarea using the style.width to ensure
the textarea is exactly the right width.
I want to set the height/rows of the textarea depending on it's
contents (so that it dynamically grows and shrinks as the contents
alter) however I can not see a way to calculate the required number of
rows (or height) for a given content string.
If all the lines of content are shorter that than the width of the
area then it's simple but obviously this is not always the case, so I
have a function that splits the content into lines then each line into

<snip>

Instead of measuring the text, compare the offsetHeight and scrollHeight of
the textarea. If the scrollHeight is the larger, you know you need to
increase the area's size.
hth
ivohttp://4umi.com/web/javascript/
thanks for that, i now have far simpler code

text_area.style.height = text_area.scrollHeight + "px";

as i want to have the area fit the contents not just increase when it
grows (ie shrink when the contents decrease)

this works fine in IE but in FF it makes the text area grow with each
char added not every row.

any ideas why?

Mar 12 '07 #3
On 12 Mar, 18:32, gilbert.geo...@whoever.com wrote:
On 12 Mar, 17:06, "Ivo" <n...@thank.youwrote:
<gilbert.geo...@whoever.comtypedI am setting the width of a textarea using the style.width to ensure
the textarea is exactly the right width.
I want to set the height/rows of the textarea depending on it's
contents (so that it dynamically grows and shrinks as the contents
alter) however I can not see a way to calculate the required number of
rows (or height) for a given content string.
If all the lines of content are shorter that than the width of the
area then it's simple but obviously this is not always the case, so I
have a function that splits the content into lines then each line into
<snip>
Instead of measuring the text, compare the offsetHeight and scrollHeight of
the textarea. If the scrollHeight is the larger, you know you need to
increase the area's size.
hth
ivohttp://4umi.com/web/javascript/

thanks for that, i now have far simpler code

text_area.style.height = text_area.scrollHeight + "px";

as i want to have the area fit the contents not just increase when it
grows (ie shrink when the contents decrease)

this works fine in IE but in FF it makes the text area grow with each
char added not every row.

any ideas why?
a-ha

this code does what i want :-)

if(isIE()) {
text_area.style.height = text_area.scrollHeight + "px";
} else {
text_area.style.height = 0;
text_area.style.height = (text_area.scrollHeight -2) + "px";
}

thanks for the help

Mar 12 '07 #4
wrote on 12 mrt 2007 in comp.lang.javascript:
this code does what i want :-)

if(isIE()) {
text_area.style.height = text_area.scrollHeight + "px";
} else {
text_area.style.height = 0;
What is the sense of the above line?
It surely will be overwritten in the next line?
text_area.style.height = (text_area.scrollHeight -2) + "px";
}

thanks for the help
this should do the trick:

text_area.style.height =
text_area.scrollHeight - ((isIE())?0:2) + "px";
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 12 '07 #5
On 12 Mar, 19:43, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
wrote on 12 mrt 2007 in comp.lang.javascript:
this code does what i want :-)
if(isIE()) {
text_area.style.height = text_area.scrollHeight + "px";
} else {
text_area.style.height = 0;

What is the sense of the above line?
It surely will be overwritten in the next line?
text_area.style.height = (text_area.scrollHeight -2) + "px";
}
thanks for the help

this should do the trick:

text_area.style.height =
text_area.scrollHeight - ((isIE())?0:2) + "px";

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
it appears that this line

text_area.style.height = 0;

is required to reset the scrollHeight to the minimum necessary to
avoid the need for a scrollbar.

without it the scrollHeight never decreases in value irrespective of
how the contents of the textarea change.

hope that makes sense
Mar 12 '07 #6
wrote on 12 mrt 2007 in comp.lang.javascript:
text_area.style.height = 0;

What is the sense of the above line?
It surely will be overwritten in the next line?
text_area.style.height = (text_area.scrollHeight -2) + "px";
}
[..]
>
it appears that this line

text_area.style.height = 0;

is required to reset the scrollHeight to the minimum necessary to
avoid the need for a scrollbar.

without it the scrollHeight never decreases in value irrespective of
how the contents of the textarea change.

hope that makes sense
It certainly does. Good point.

text_area.style.height = 0; // remove at your peril!!

Is this hat tric valid for reasonably all non-IE browsers?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sticky | last post by:
I really need some help - i have already pulled out 1/2 my hear and the rest is following quickly..... I am designing a page containing an iFrame, the contents of which are selected dynamically...
13
by: Jon Yeager | last post by:
I need to display a bunch of pictures that are all of various dimensions into a fixed dimension space, like MSN Messenger does with its user photos in the chat windows. Forcing image dimensions...
1
by: Nikhil Patel | last post by:
Hi all, I dynamically create new controls and display them on the form. My problem is that the user has to resize form manually to see controls whose location property points to a point outside...
2
by: UmmagummA | last post by:
Any idea, how to dynamically resize controls on the form when, the form is resized, let say maximized? Is there any elegant, logical way to do this
1
by: TheBinar | last post by:
Hi I have a user control that contains a RichTextBox in vb.net. In my program, I create multiple instances of this control (with the RichTextBox being in each one), that appear one above the...
0
by: RateTheBuilder | last post by:
Hi I found the below scripts which are used to dynamically resize the height of the text area input control. The site I found it on www.webdeveloper.com had it being called with the following...
64
by: Philip Potter | last post by:
Hello clc, I have a buffer in a program which I write to. The buffer has write-only, unsigned-char-at-a-time access, and the amount of space required isn't known a priori. Therefore I want the...
3
by: =?iso-8859-1?q?Jean-S=E9bastien?= | last post by:
hello, is it possible to resize an image with same proportion. having a maximum height and a maximum width. the solution should work on main browsers (do not use max-height and max-width). ...
3
by: Phil Endecott | last post by:
Dear Experts, It looks as if the HTML4 spec does not define a meaning for empty height and width attributes in an IMG element. Moz seems to ignore them, while IE7 sets the dimension to 1 pixel...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...
0
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...
0
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,...

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.