/    Sign up×
Community /Pin to ProfileBookmark

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>
[/code]

(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>
[/code]

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.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JUDMay 09.2007 — Couldn't you just do this:

[code=php]
<script type="text/javascript">
function replaceChars(entry) {
out = "a"; // replace this
add = "z"; // with this
temp = entry.replace(out, add);

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>
[/code]


That seems a lot easier to me
Copy linkTweet thisAlerts:
@AnnaccondauthorMay 09.2007 — That seems to work ? but could I use something like this:

[code=php]<script language="javascript">
<!--
function change(item,item2) {
var range = document.body.createTextRange();
range.collapse(true);
if (location.href.indexOf("action=") != -1 ) {
return false;
} else {
while (range.findText(item)) {
range.text=item2;
range.collapse(false);
}
}
}
change("a","z");
change("b","x");
// -->
</script>[/code]


It would be less code because every item1 would be changed in item2 (and I have many values to replace). However this code seems to not work ? Any idea how to make it work?
Copy linkTweet thisAlerts:
@AnnaccondauthorMay 10.2007 — I checked all and it doesn't work at all for multiple values:

<script type="text/javascript">

function replaceChars(entry) {

temp = entry.replace("old", "new");

document.subform.text2.value = temp;

}

</script> [/QUOTE]


[B]Any ideas how to change many values in 1 string?[/B]
Copy linkTweet thisAlerts:
@JUDMay 10.2007 — Sorry, made a little bit of a booboo. You need to pass a regular expression to the replace method like this:

[code=php]
<script type="text/javascript">
function replaceChars(entry) {
out = /a/g; // replace this
add = "z"; // with this
temp = entry.replace(out, add);

document.subform.text2.value = temp;
}
</script>
[/code]


See it working HERE
Copy linkTweet thisAlerts:
@AnnaccondauthorMay 10.2007 — Yes I realized it already because I read many tips about strings today however still can't find what I need. This example showing only how to replace 1 value in string to another (a to z) but how I can replace more values in the same string, like: "a" to "z", "b" to "x" etc. All in one string?
Copy linkTweet thisAlerts:
@JUDMay 10.2007 — Try this:

[CODE]
<script type="text/javascript">
function replaceChars(entry) {
var arrReplace = new Array();
arrReplace[0] = [/a/g, "z"];
arrReplace[1] = [/b/g, "y"];
arrReplace[2] = [/c/g, "x"];

for(var i = 0; i < arrReplace.length; i++){
entry = entry.replace(arrReplace[i][0], arrReplace[i][1]);
}
document.subform.text2.value = entry;
}
</script>
[/CODE]


[SIZE="6"]CLICKY[/SIZE]
Copy linkTweet thisAlerts:
@AnnaccondauthorMay 10.2007 — Working perfect ? Thank you very much.
×

Success!

Help @Annaccond spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...