2

I was trying to iterate in loop through text boxes and assign values to it. I have created three array list ids[],id1s[],id2s[].

values in ids[] have to be populated in the first column and its coming fine. but values in id1s[] and id2[] need to be populated in the 2nd and 3rd column. loop run 5 times. first times it populating in the first row, but from next time onward its overriding the first row.. and not coming to second row

Below is my code :--

<!DOCTYPE html>
  <html>
    <head>
      <script>
        function start()
        {
          var ids = ["A", "B", "C", "D", "E"];
          var id1s = [1,2,3,4,5];
          var id2s = [6,7,8,9,10];
          for (var i = 0; i < ids.length; i++){
            var value=ids[i];
            addrow(value);
            addData(id1s[i], id2s[i]);
          }
        }

        function addrow(value)
        {
          var test1 = value+'1';
          var test2 = value+'2';
          var TABLE = document.getElementById('tableId');
          var BODY = TABLE.getElementsByTagName('tbody')[0];
          var TR = document.createElement('tr');
          var TD1 = document.createElement('td');
          var TD2 = document.createElement('td');
          var TD3 = document.createElement('td');
          TD1.innerHTML = value;
          TD2.innerHTML = "<input type='text' id='test1' value=''>";
          TD3.innerHTML = "<input type='text' id='test2' value=''>";
          TR.appendChild (TD1);
          TR.appendChild (TD2);
          TR.appendChild (TD3);
          BODY.appendChild(TR);
        }

        function addData(num,num1)
        {
          alert("Showing assignment of values");
          document.getElementById("test1").value=num;
          document.getElementById("test2").value=num1;
        }
      </script>
    </head>
  <body>

  <table id="tableId" border='1' cellspacing='0' cellpadding='0'>
  <tr>
    <td>Variable</td> 
    <td>Value1</td>
    <td>Value2</td>
  </tr>
  <button type="button" onclick="start()">Display</button>                  
  </table>

</body>

please suggest how to iterate through the text boxes created in 2nd and 3rd column

2 Answers 2

1

I modified your addrow and addData functions

For addrow function You have to use the value inside test1 and test2 to form the input ids like this

TD2.innerHTML ="<input type='text' id='"+ test1 + "' value=''>";
TD3.innerHTML ="<input type='text' id='"+ test2 + "' value=''>";

for addData function bring in the 'value' variable as a parameter and change the document.getElementById statements as

document.getElementById(value + "1").value=num;
document.getElementById(value + "2").value=num1;

Please check jsFiddle http://jsfiddle.net/TbGcD/1/

Sign up to request clarification or add additional context in comments.

Comments

0

In my experience, something like this should provide decent performance

<!DOCTYPE html>
    <html>
        <head>
        <script>
        function start()
        {
            var ids = ["A", "B", "C", "D", "E"];
            var id1s = [1,2,3,4,5];
            var id2s = [6,7,8,9,10];
            var table= document.getElementById('tableId').getElementsByTagName('tbody')[0];

            for (var i = 0; i < ids.length; i++){
                alert("Showing assignment of values");
                table.innerHTML+= '<tr><td>'+ids[i]+'</td><td>'+id1s[i]+'</td><td>'+id2s[i]+'</td></tr>';
            }
        }
        </script>
    </head>
    <body>
    <button type="button" onclick="start()">Display</button>
    <table id="tableId" border='1' cellspacing='0' cellpadding='0'>
        <tr>
            <td>Variable</td> 
            <td>Value1</td>
            <td>Value2</td>
        </tr>
    </table>
</body>

If you don't need to see each row assigned separately, I suggest doing something like this

<!DOCTYPE html>
    <html>
        <head>
        <script>
        function start()
        {
            var ids = ["A", "B", "C", "D", "E"];
            var id1s = [1,2,3,4,5];
            var id2s = [6,7,8,9,10];
            var table= document.getElementById('tableId').getElementsByTagName('tbody')[0];
            var text='';

            for (var i = 0; i < ids.length; i++){
                text+='<tr><td>'+ids[i]+'</td><td>'+id1s[i]+'</td><td>'+id2s[i]+'</td></tr>';
            }
            table.innerHTML+= text;
            text='';
        }
        </script>
    </head>
    <body>
    <button type="button" onclick="start()">Display</button>
    <table id="tableId" border='1' cellspacing='0' cellpadding='0'>
        <tr>
            <td>Variable</td> 
            <td>Value1</td>
            <td>Value2</td>
        </tr>
    </table>
</body>

1 Comment

thanks buddy. the only thing was that the second and third column was supposed to be text boxes. Now i am able to do that by a reply from saju in this thread...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.