Click to See Complete Forum and Search --> : Carriage Return


saroye
08-25-2003, 02:15 PM
New to JS - much ignorance still...

Trying to create an onclick funtion that will create a carriage return after each period (at the end of each sentence) for sentences typed into a textarea box.

Here's what I am currently working with:

<script type="text/javascript">

var period = '.';
function addCR() {
document.bob.test.value = document.bob.test.value + period + "\n";
}
function slf()
{
var string1=document.bob.test.value
if (string1.indexOf(".")==+1)
{
addCR()
}
}
</script>


</head>

<body>

<form name="bob">
<textarea name="test" rows="5" cols="20">Some text</textarea>
<input type="button" value="slf" onClick="slf()">
</form>

Any help most appreciated before I put the gun in my mouth...

Thanks.

pyro
08-25-2003, 02:21 PM
Try this:

<script type="text/javascript">
function slf()
{
var string1 = document.bob.test.value;
string1 = string1.replace(/\.(\s)*/,".\n");
document.bob.test.value = string1;
}
</script>
</head>
<body>
<form name="bob">
<textarea name="test" rows="5" cols="20">Some text</textarea>
<input type="button" value="slf" onClick="slf()">
</form>

saroye
08-25-2003, 03:04 PM
Works, but only for the first sentence typed - it leaves other sentences intact. Any ideas?

Thank you for help!

pyro
08-25-2003, 03:35 PM
Yes, make the regexp global:

string1 = string1.replace(/\.(\s)*/g,".\n");

saroye
08-25-2003, 03:54 PM
Awesome!!! Works like a champ. Thanks a ton!

S

PS - I noticed that you have a web hosting company. My wife has one too and we are getting ready to move to India soon to establish QUALITY tech support for other hosting companies (huge market when you add the word quality).

pyro
08-25-2003, 04:01 PM
Glad I could help...

Originally posted by saroye
huge market when you add the word quality...The real trick is actually providing quality service... ;)