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