I have some problem with replacing multiple words in forms. I have 2 text fields and I want to after write something in first text field and click on button to some words be replaced and text be showed in second text field (with those replaced words).
I found some way how to replace single value:
[code=php]
<SCRIPT LANGUAGE=”JavaScript”>
function replaceChars(entry) {
out = “a”; // replace this
add = “z”; // with this
temp = “” + entry; // temporary holder
while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = “” + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
document.subform.text2.value = temp;
}
</script>
<form name=”subform”>
<input type=text name=text size=40 value=”abcdabcd”><br>
<input type=text name=text2 size=40 value=””>
<input type=button name=action value=”Done!” onClick=”replaceChars(document.subform.text.value);”>
</form>
(but this script seems to work only for single characters)
or
[code=php]
<script language=”JavaScript”>
function replaceCharacters() {
var origString = document.form1.inTB.value;
var inChar = document.form1.inC.value;
var outChar = document.form1.outC.value;
var newString = origString.split(inChar);
newString = newString.join(outChar);
document.form1.outTB.value = newString;
}
</script>
<form name=”form1″ method=”post” action=””>
<input name=”inTB” type=”text” id=”inTB” value=”Original text string” size=”30″>
<br>
Replace all instances of:
<input name=”inC” type=”text” id=”inC” value=”tr” size=”4″>
with:
<input name=”outC” type=”text” id=”outC” value=”w” size=”4″><br>
<input type=”button” name=”Capitalize” value=”Replace Now” onClick=”replaceCharacters();”><br>
<input name=”outTB” type=”text” id=”outTB” value=”” size=”30″>
</form>
I think this second script would work good but I don’t know how to change it to make it work with replacing many values at once. Maybe it’s necessary create other script.
I need as simple way as possible because it have to replace many words so it would be great if code won’t be very long and complicated ? I’ll appreciate any help.