469,330 Members | 1,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

how to force carriage return in textarea

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

tks
Jul 23 '05 #1
8 45435
Steven wrote:
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2

"Randy Webb" <Hi************@aol.com> wrote in message
news:qZ********************@comcast.com...
Steven wrote:
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.

whta's the carriage return?

sTxtBoxValue = sTxtBoxValue + "\r"?


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3
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

Jul 23 '05 #4

"Mick White" <mw***********@rochester.rr.com> wrote in message
news:sU****************@twister.nyroc.rr.com...
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>"?


<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>


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

Jul 23 '05 #5
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
Jul 23 '05 #6
Steven wrote:
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.
Jul 23 '05 #7
Mick White wrote:

<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
Jul 23 '05 #8
Mick White wrote:
Mick White wrote:

<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>


I have a script somewhere that does what the OP Wants. But it works
onChange. These are the basic steps:

1) Read the value.
2) Find the nth character, where n is the desired width in characters.
3) Is the nth character a space?
If not a space: find the lastindexOf a space.Grab the substrings.
If a space:
Grab a substring from 0 to n-1, grab the substring from n to string length.
Save the first substring in an array.
Repeat with the remaining string.

Now, put your array back together with a new line character with join().
Put the string back into the textarea.

It will only work with a fixed width font.

Ahh, here it is:
http://members.aol.com/_ht_a/hikksno...ypingText.html

Its primitive at best (could be refined). But it uses a DIV tag and BR/P
tags instead of \n\r but the insertBR function could easily be modified
to work with newlines.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Canes_Rock | last post: by
2 posts views Thread by Andrew Chanter | last post: by
1 post views Thread by VMI | last post: by
3 posts views Thread by Dinsdale | last post: by
11 posts views Thread by evenlater | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.