Click to See Complete Forum and Search --> : Help with scripting


pgo
September 14th, 2003, 09:59 AM
Hi,

I am having a problem with scripting and am hoping someone can help me. (I know it is probably a stupid question but I am new to programming.)

I want a function which reads a text string and copies it to a new string with all underscores (_) converted to spaces.

here is what I have right now

function text_test(text)
{
text1 = new Array();
len = text.length;
for (a=0;a<len;a++) {
if (text.charAt(a) == '_') text1.charAt(a) = ' ';
else text1.charAt(a) = text.charAt(a);
}
document.writeln(text1);
}

I would appreciate any help anyone could give.

many thanks,

Per-Gunnar

n_p_k76
September 15th, 2003, 04:29 AM
Try this :



<HTML>
</HEAD>
<SCRIPT Language="JavaScript">
function text_test(text){
//Don't have to use an array, simple string will do.
var text1 = '';//new Array();
len = text.length;

for (var a=0;a<len;a++) {
if (text.charAt(a) == '_')
text1 = text1 + ' ';
else
text1 = text1 + text.charAt(a);
}
//document.writeln(text1);
document.frm1.txt2.value = text1;
}

</SCRIPT>
</HEAD>
<BODY>
<Form name="frm1">
<Input type="text" name="txt1"/>
<BR/>
<INPUT TYPE="Button" name="btn1" value="BUTTON" onClick="text_test(document.frm1.txt1.value);"/>
<BR/>
<Input type="text" name="txt2"/>
</Form>
</BODY>
</HTML>

pgo
September 15th, 2003, 06:11 AM
Excellent. Thanks