PDA

View Full Version : Split up text in textarea


Gene
10-15-2002, 08:43 PM
Please take a look at this link:
http://www.onlinetools.org/tools/textsplitdata/index.php

I need to do exactly this, but in JavaScript.

I need to split the text in a textarea after 250 characters, and transfer the value of whatever comes after the 250 characters into a different var in the form. Any help would be greatly appreciated.

mordred
10-15-2002, 09:29 PM
I suppose you want to transfer the part of the text after the first 250 characters to another textarea in the form, not a var. Var(iables) usually only exist in scripts and such. You might give this code a shot:


<html>
<head>
<title>foo</title>
<script>
function sliceValues(formRef, fieldA, fieldB, startIndex) {
// store the value of the first textarea in a variables
var text = formRef.elements[fieldA].value;

// if text length is long enough, store the sliced portion
// in textfield two (fieldB)
if (text.length > 250) {
formRef.elements[fieldB].value = text.substring(startIndex);
}
}
</script>
</head>

<body>

<form onsubmit="sliceValues(this, 'fieldA', 'fieldB', 250); return false;">
Field A <textarea name="fieldA" style="height:100px; width:400px;">
</textarea>
<br />
<br />
Field B <textarea name="fieldB" style="height:100px; width:400px">
</textarea>
<br />
<br />
<input type="submit" value="wrap words" />
</form>

</body>
</html>

Gene
10-15-2002, 10:10 PM
Thanks mordred,

This is exactly what I wanted. You've been a great help. Thanks a million.

One last question. How to I get the value of the text before the 250 characters?

ConfusedOfLife
10-15-2002, 10:30 PM
if (text.length > 250)
formRef.elements[fieldB].value = text.substring(startIndex);




Change it to :

if (text.length > 250)
formRef.elements[fieldB].value = text.substring(startIndex, 0);

Gene
10-15-2002, 11:07 PM
Thank you both very much!!!