Additional help needed with preserving textarea CR/LF

Z

zjw2112

Hello. I have some javascript code that dynamically creates a textarea
and sets the wrap value to hard, which I thought would preserve CR/LF
in the textarea:

var otherTextArea = document.createElement("textarea");
otherTextArea.setAttribute("rows", "10");
otherTextArea.setAttribute("cols", "30");
otherTextArea.setAttribute("id", "otherTextArea");
otherTextArea.setAttribute("wrap", "hard");

At the appropriate time, I add the text string in the textarea to an
XML string using the following:

var textArea = document.getElementById("otherTextArea");
commentsXML += "<other>" + textArea.value + "</other>";

Using the following example, which would be in a textarea:

aaa
bbb
ccc

where 'aaa', 'bbb', and 'ccc' all have CR/LF after them, the number of
characters of textArea.value is 15, as expected. I have to send this
XML string to a JSP page for processing and return, therefore I use
AJAX to accomplish this. However, on the server-side of things, when I
get the length between
the XML string element <other>aaabbbccc</other>, the length is 9, i.e.
the CR/LF is not preserved. Does anyone know what I am doing wrong?
I've tried setting the wrap to off, but this didn't help either. Thank
you.
 
V

VK

Hello. I have some javascript code that dynamically creates a textarea
and sets the wrap value to hard, which I thought would preserve CR/LF
in the textarea:

It wouldn't, but it may add extra line breaks in the submitted text.
What wrap attribute does is handling the lines wrapping inside the
textarea.
wrap="off" theoretically prevents wrapping, so all user input goes in
one line until she presses Enter.
wrap="soft" theoretically default theoretically does what you normally
see in textarea: lines are wrapping within the textarea borders by only
user entered line breaks are part of textarea value
wrap="hard" theoretically has the same visual effect as wrap="soft" but
all automated wraps are part pf textarea value - together with the
"real" user-typed line breaks.

I'm saying "theoretically" everywhere because "wrap" is a rather funny
attribute: it is supported somehow by all browsers starting from NN/IE
3rd ver. yet it never was standardized and included in any W3C DTD's.
So whatever however it does on a particular UA - be happy with it, as
you have neither basis nor authority to complain.

But in application in your case as I said you don't need to "preserve
CR/LF" in textarea and in anyway wrap="hard" has no business to it:
unless you are trying to preserve soft wraps as physical line breaks(?)
 
Z

zjw2112

I need to preserve the physical line-breaks, because I store the
textarea text into the database, and later, read it from the database
and load it into another textarea, therefore I want the string to look
as it was entered.

Thank you.
 
D

Danny

Not too sure how you're checking the string, but the cr/lf in a text/plain
input box will be \n, same in a textarea, and there's a standard way, which
works on all more or less, YOUROBJ.style.whiteSpace='pre'; pre as in
preSERVING as is, as in a <pre> element, the CSS being {white-space: pre}.

Danny
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top