How do I dynamically size a <HTML:TEXTAREA>????

A

Anders S. Clausen

Hi

I have a jsp with a couple of <HTML:TEXTAREA> where I would like to
dynamically size the textarea depending on the length of a String entered on
a different jsp. The two new textareas are to display the full text (and no
scroll bars), and I was wondering what I set the row and cols to ....
keeping in mind that I have the length of the text I want to display.
Any ideas???

Cheers.

Anders
 
T

Toby A Inkster

Anders said:
he two new textareas are to display the full text (and no
scroll bars), and I was wondering what I set the row and cols to ....

Wouldn't <pre> make more sense than <textarea>?
 
B

Bertilo Wennergren

Anders said:
I have a jsp with a couple of <HTML:TEXTAREA> where I would like to
dynamically size the textarea depending on the length of a String entered on
a different jsp. The two new textareas are to display the full text (and no
scroll bars), and I was wondering what I set the row and cols to ....
keeping in mind that I have the length of the text I want to display.

You don't really have the lenght of the text, if you mean the width and
height it will take to display it, since you can't know what font and
font size will be used. If you're lucky, a fixed width font will be
used, which makes it easier, be you can't count on that.
 
M

Matthias H. Risse

hi,
simply access the style-elements you usually
set statically in your css.

/* resize specific textfield */
function resizeElement(x,y)
{
document.formName.elementName.style.height = y;
document.formName.elementName.style.width = x;
}


this would be more elegant / flexible, but is untested:

/*
* @param id id of to be modified element
* @param x new width in pixel
* @param y new height in pixel
*/
function resizeElement(id,x,y)
{
var a = 30;
var b = 30;

document.getElementById(id).style.height = y-a;
document.getElementById(id).style.width = x-b;
}

you could e.g. call this in <body> tag on events onLoad and/or
onResize, depending on your needs. remember to give your textarea
an id="foo" attriute for this one.

yours
matthias
 
J

J.P.Jarolim

Hi.

There are two things to consider: The length of the longest single line and
the count of all lines in total.
So if you have both, you can set the width and the length of the textarea
accordingly.
You should set the font of the textarea (style) to courier too.

You can get the single lines of the given text by finding all newlines and
carriage return combinations.
For example with the package gnu.regexp:

String inputText = getItFromSomewhere();
gnu.regexp.RE re = new gnu.regexp.RE("(.*?)[\\n\\r]+",
gnu.regexp.RE.REG_MULTILINE);
gnu.regexp.REMatch[] lines = re.getAllMatches(inputText);
int longestLine = 0;
for (int i=0; i<lines.length; i++) {
String currentMatch = lines.toString(1);
if (currentMatch.length() > longestLine) longestLine =
currentMatch.length();
}
out.println("<textarea cols=" + longestLine + " rows=" + lines.length + ">"
+ inputText + "</textarea>");

just my thoughts,

J.P.Jarolim
 
J

Jukka K. Korpela

Toby A Inkster said:
Wouldn't <pre> make more sense than <textarea>?

Partly. It would avoid misleading the user into thinking that there is a
text input area, which is what textarea is for.

But there's usually no reason to use clumsy preformatted text in HTML at
all. Why would anyone write plain text formatted to specific line length
when you can do things the HTML way, with paragraph markup and all that?

As usual, posting a description of a problem, rather than a misguided
attempt to approach an unspecified problem, would probably lead to a good
solution. Especially if pointless crossposting is omitted.

Followups trimmed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top