0
votes

With reference to the question regarding replicating values from one textbox to another

Replicating the values to another textbox

The solution works well if there are only two text boxes. But it fails when the index is running on loop.

For Ex - let us consider the table has list of text boxes in each row

1st row textbox1 textbox2
2nd row textbox3 textbox4

the id for each box is generated based on the status index. When I update the value in textbox1 textbox2 gets updated. But when I update textbox3 , instead of updating textbox4.textbox2 is updated.

2

2 Answers

0
votes

Because the linked code updates textbox2 directly. Put the ids in an array and iterate over it to set the value of each text box, e.g.

function updateTextareas(el) {
  var value = el.value;
  for (var i=1, iLen=arguments.length; i<iLen; i++) {
    document.getElementById(arguments[i]).value = value;
  }
}

Then you can call it like:

document.getElementById('input1').onkeyup = function () {
  updateTextareas(this,'textbox2','textbox3',...);
};

or some similar strategy.

0
votes

Give all elements in the set the same name (or classname, and use getElementsByClassName). Then do the following pseudocode in javascript:

// onmodify:

var name = thisTextarea.name;
var newText = thisTextarea.text;
var elementsToUpdate = document.getElementsByName(name);

for each textarea in elementsToUpdate {
    if (textarea != thisTextarea) // may not even need this line
        textarea.text = newText;
}