473,657 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 26986
Ivo
<gi************ @whoever.comtyp ed
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.you wrote:
<gilbert.geo... @whoever.comtyp edI 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.scrol lHeight + "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.you wrote:
<gilbert.geo... @whoever.comtyp edI 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.scrol lHeight + "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.scrol lHeight + "px";
} else {
text_area.style .height = 0;
text_area.style .height = (text_area.scro llHeight -2) + "px";
}

thanks for the help

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

if(isIE()) {
text_area.style .height = text_area.scrol lHeight + "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.scro llHeight -2) + "px";
}

thanks for the help
this should do the trick:

text_area.style .height =
text_area.scrol lHeight - ((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.net wrote:
wrote on 12 mrt 2007 in comp.lang.javas cript:
this code does what i want :-)
if(isIE()) {
text_area.style .height = text_area.scrol lHeight + "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.scro llHeight -2) + "px";
}
thanks for the help

this should do the trick:

text_area.style .height =
text_area.scrol lHeight - ((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.javas cript:
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.scro llHeight -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
10141
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 by the user, and it needs to resize correspondingly - both height and width. the width isn't a problem; it is set from its parent object using width=100%, but the height is a killer. Basically the user selects a menu item and the src of the...
13
6879
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 will warp the proportions, so that's no good. Forcing one coordinate while leaving the other flexible will maintain proportions, but the x,y values won't be consistant from picture to picture. I considered the MSN Messenger method of bringing...
1
4871
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 the form's area. So can you please show me a way to resize the form after adding a dynamic control so whose Location points to a point outside the form's boundary. So that the control can be displayed without resizing the form manually? Thanks...
2
13310
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
5443
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 other on a panel. The problem is that each control may have varying amounts of data, and so each RichTextBox (within each control) needs to size to fit the amount of text within that RichTextBox. I've tried multiplying the size of a single line...
0
1442
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 event of the text area control: onfocus="adjustRows(this)" The problem is I want these to be called when the control is loaded rather than onFocus. There can be any number of these text areas on the page as I have them inside of a datagrid which...
64
9708
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 buffer to dynamically grow using realloc(). A comment by Richard Heathfield in a thread here suggested that a good algorithm for this is to use realloc() to double the size of the buffer, but if realloc() fails request smaller size increments...
3
8846
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). or should i do it with javascript?
3
5309
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 (or something like that). I have a vague recollection that "" did have a defined meaning at some point in the past, but maybe I am mis-remembering. Can anyone confirm? I have encountered this in some third-party PHP code that I'm using on
0
8402
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
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
8829
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...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5633
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
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2733
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
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.