/    Sign up×
Community /Pin to ProfileBookmark

Javascript code to replace values in a string with values from an array

Hello everybody

I have a string that looks like this:

<input type=’checkbox’ name=’list’ class=’indent-28′ value = ‘ABC’ name=’Netherlands’>
<label>Netherlands</label><br>

<input type=’checkbox’ name=’list’ class=’indent-28′ value = ‘DEF’ name=’Germany’>
<label>Germany</label><br>

<input type=’checkbox’ name=’list’ class=’indent-28′ value = ‘GHI’ name=’Italy’>
<label>Italy</label><br>

<input type=’checkbox’ name=’list’ class=’indent-44′ value = ‘JKL’ name=’Brazil’>
<label>Brazil</label><br>

<input type=’checkbox’ name=’list’ class=’indent-44′ value = ‘MNO’ name=’Argentina’>
<label>Argentina</label><br>

<input type=’checkbox’ name=’list’ class=’indent-44′ value = ‘PQR’ name=’Argentina’>
<label>Argentina</label><br>

I also have two JavaScript arrays which are dynamically generated in accordance with the checkboxes the end user selects and with the respective values he/she wants to enter for these selected items.

So, assuming the user selects “Netherlands”,”Germany”,”Argentina” and “Argentina” from the above list, the first array will be [“Netherlands”,”Germany”,”Argentina”,”Argentina”].

Furthermore, if the user wishes to modify the values of these attributes, he/she can do so by entering new values in text boxes that I’ve provided in the application.
(In this case let’s say X,Y,Z,W are the new values the user wishes to enter. Hence, the second array will be [“X”,”Y”,”Z”,”W”])

Now, I have both the arrays and I just want to replace the values of the attributes in the above string with those in the modified array.

The desired output/end result would be something like this:

<input type=’checkbox’ name=’list’ class=’indent-28′ value = ‘X’ name=’Netherlands’>
<label>Netherlands</label><br>

<input type=’checkbox’ name=’list’ class=’indent-28′ value = ‘Y’ name=’Germany’>
<label>Germany</label><br>

<input type=’checkbox’ name=’list’ class=’indent-28′ value = ‘GHI’ name=’Italy’>
<label>Italy</label><br>

<input type=’checkbox’ name=’list’ class=’indent-44′ value = ‘JKL’ name=’Brazil’>
<label>Brazil</label><br>

<input type=’checkbox’ name=’list’ class=’indent-44′ value = ‘Z’ name=’Argentina’>
<label>Argentina</label><br>

<input type=’checkbox’ name=’list’ class=’indent-44′ value = ‘W’ name=’Argentina’>
<label>Argentina</label><br>

X,Y,Z and W should ideally replace the previous values ABC,DEF,MNO and PQR respectively in the string.

Can anybody help me with the code??

I tried with the following code snippet but its not working..
[Note: selectedAttributeNames is the first array and ModifiedValues is the second one.]

Code:

var str= “<input type=’checkbox name=’list’……………… // This is the original string

for (var i = 0; i < selectedAttributeNames.length; i++) {

var find = new RegExp(“value='[^’]*’ name='” + selectedAttributeNames[i] + “‘”);
var repl = “value='” + ModifiedValues[i] + “‘ shname='” + selectedAttributeNames[i] + “‘”;

str = str.replace(find, repl);

}

str = str.replace(/ shname=/, ” name=”);

alert(str);

The alert function displays/returns the string in it’s original state. !! Please help!

Thanks in advance

Abhirup

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@007JulienMay 08.2014 — Inputs have two name attributes with different values.

Replace your code with something like this (with the country in the class tag)
[CODE]<input type='checkbox' name='list' class='indent-28 Netherlands' value = 'ABC'>
<label>Netherlands</label><br>
<input type='checkbox' name='list' class='indent-28 Germany' value = 'DEF'>
<label>Germany</label><br>
<input type='checkbox' name='list' class='indent-28 Italy' value = 'GHI'>
<label>Italy</label><br>
<input type='checkbox' name='list' class='indent-44 Brazil' value = 'JKL'>
<label>Brazil</label><br>
<input type='checkbox' name='list' class='indent-44 Argentina' value = 'MNO'>
<label>Argentina</label><br>
<input type='checkbox' name='list' class='indent-44 Argentina' value = 'PQR'>
<label>Argentina</label><br>[/CODE]


Then make a loop with getElmentsByTagName('input'), spit the class and find the indexOf of the country in your country array to change the value with the same index (if !=-1) in the second array.
×

Success!

Help @Abhirup 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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