how to force carriage return in textarea

S

Steven

i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?

tks
 
R

Randy Webb

Steven said:
i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?

onchange, read its value, insert the returns, and move on.
 
M

Mick White

Steven wrote:

whta's the carriage return?

sTxtBoxValue = sTxtBoxValue + "\r"?
It depends where you mean to display the text. Normally you don't need
to insert carriage returns, but if it's for display in HTML page, and
you need to force a break.


sTxtBoxValue = sTxtBoxValue + "<BR>"?


But where to place the break can become problematic.

You may need to transform the text into an array.

arr=text.split(/\s+/)
Then count the characters in each array entry, and set up some kind of
"while" loop.
Mick
Mick
 
S

Steven

Mick White said:
Steven wrote:


It depends where you mean to display the text. Normally you don't need
to insert carriage returns, but if it's for display in HTML page, and
you need to force a break.


sTxtBoxValue = sTxtBoxValue + "<BR>"?

<br> won't work inside a textarea. i need to start a new line withing
textarea, just as though the user has hit the enter key. in vb, i could use
a 'sendkeys' function to mimic keyboard behaviour. i need to do the same
thing here. this doesn't work:

<script language="javascript">
function forceReturn(iMaxLength, sValue){
if (sValue.length > iMaxLength){
sValue = sValue + "\r";
}
}
</script>

<textarea name="txt" onKeyUp="forceReturn('5', this.value);" rows="5"
cols="10"></textarea>
 
M

Mick White

Steven wrote:
[snip]
<script language="javascript">
function forceReturn(iMaxLength, sValue){
if (sValue.length > iMaxLength){
sValue = sValue + "\r";
}
}
</script>

<textarea name="txt" onKeyUp="forceReturn('5', this.value);" rows="5"
cols="10"></textarea>

<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length > iMaxLength){
sValue.value += "\r";
}
}
</script>

<textarea name="txt" onKeyUp="forceReturn('5', this);" rows="5"
cols="10"></textarea>

You can't change the value using :
sValue = sValue + "\r";
(sValue is a reference not an object). Better to pass the textfield
object to your function.

Mick
 
R

RobG

Steven said:
i need to force a carriage return with a textarea field at X number of
characters. anybody know how to do this?

You can't reliably do this, though you may be able to get it to work in
a particular browser, but certainly not all.

One of the biggest problems is that you have no idea where the
insertion point is. You can't simply add a return after 5 keystrokes,
or to the end of the string if it's more than 5 characters long.

Say you add an onkeydown event the checks the number of characters, and
when the user types a 5th character, you add a return to the end of the
string to add a 6th character.

But the cursor is still at the 5th, before your return, so any further
input will go before your return - you can't move the insertion point
programmatically.

Another is if the user puts the insertion point somewhere else in the
string and starts typing - say changing the first 5 character phrase.
you now must get rid of all your returns and put them back in the right
place - each time a key is pressed.

There are many more scenarios where this will fail, as no doubt you
have discovered.

Rob.
 
M

Mick White

Mick said:
<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length > iMaxLength){
sValue.value += "\r";
}
}
</script>
The above script is nonsense, sorry. Notwithstanding Rob's caveats,
the following is an improvement (It will create a column of text 5
characters wide)

<script type="text/javascript">
function forceReturn(iMaxLength,sValue){
if (sValue.value.length % iMaxLength==5){
sValue.value += "\r";
}
}
</script>
Mick
 

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
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top