pn1978
12-08-2006, 08:51 AM
I found on the web JS code to insert a break inside a textbox at the specified character count. The script does what I need except on major thing, it does not check if the specified character is a space. What I need the script to do is to check if the specified character is a space, if it is it should proceed, if it is not, I want it to look backwards for the first space and insert a break there. I only have some html and ColdFusion background. I tried couple of loops and if statements but it does not work. Any suggestions or pointers? Any help is appreciated.
Thanks.
example of the code
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function showLines(max, text) {
max--;
text = "" + text;
var space = " ";
var temp = "";
var chcount = 0;
for (var i = 0; i < text.length; i++) // for each character ...
{
var ch = text.substring(i, i+1); // first character
var ch2 = text.substring(i+1, i+2); // next character
if (ch == '\n') // if character is a hard return
{
temp += ch;
chcount = 1;
}
else
{
if (chcount == max) // line has max chacters on this line
{
if (ch == " ")
{
temp += '\n' + ch; // go to next line
chcount = 1; // reset chcount
}
else
{
var lastSpace = temp.lastIndexOf('w', 1);
alert(lastSpace);
alert(temp)
//return();
}
}
else // Not a newline or max characters ...
{
temp += ch;
chcount++; // so add 1 to chcount
}
}
}
return (temp); // sends value of temp back
}
// End -->
</script>
<textarea name="test" cols="55" rows="5" onChange="javascript:this.form.test.value = showLines(55, this.form.test.value); this.value=this.value.toUpperCase();"></textarea>
Thanks.
example of the code
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function showLines(max, text) {
max--;
text = "" + text;
var space = " ";
var temp = "";
var chcount = 0;
for (var i = 0; i < text.length; i++) // for each character ...
{
var ch = text.substring(i, i+1); // first character
var ch2 = text.substring(i+1, i+2); // next character
if (ch == '\n') // if character is a hard return
{
temp += ch;
chcount = 1;
}
else
{
if (chcount == max) // line has max chacters on this line
{
if (ch == " ")
{
temp += '\n' + ch; // go to next line
chcount = 1; // reset chcount
}
else
{
var lastSpace = temp.lastIndexOf('w', 1);
alert(lastSpace);
alert(temp)
//return();
}
}
else // Not a newline or max characters ...
{
temp += ch;
chcount++; // so add 1 to chcount
}
}
}
return (temp); // sends value of temp back
}
// End -->
</script>
<textarea name="test" cols="55" rows="5" onChange="javascript:this.form.test.value = showLines(55, this.form.test.value); this.value=this.value.toUpperCase();"></textarea>