title: Dynamic adding input fields into columns
body: <p>I had this block of code in javascript which does add dynamic input fields. Every thing works fine at this point. My question is how would I add more input fields within the table in other cell respectively with the village name input fields? When user Add Village 1/Remove Village 1, either of the action should add/remove aligning input field on column named:Type of participatory forest management, Year participatory management process started and Size of forest. The input fields increment of the columns mentioned above should reflect the increment of dynamic input field of village name column. Later I will use the dynamic input fields with Php on sending the values to database. Thanks for your time!</p>
  
  <p>Below is script:</p>
  
  <p><strong>script code:</strong></p>
  
  <pre><code>    &lt;script language="JavaScript" type="text/javascript"&gt;
  
  function getById(e){return document.getElementById(e);}
  function newElement(tag){return document.createElement(tag);}
  function newTxt(txt){return document.createTextNode(txt);}
  
  function addForestName()
  {
      var tbl = getById('tblSample'); //note the single line generic functions written above
      var lastRow = tbl.rows.length;
      // if there's no header row in the table, then iteration = lastRow + 1
      var iteration = lastRow;
      var row = tbl.insertRow(lastRow);
  
      // Column which has iteration count
      var cell_no = row.insertCell(0);
      var textNode = newTxt(iteration);
      cell_no.appendChild(textNode);
  
      // Column which has the forest name
      var frstNameCell = row.insertCell(1);
  
      var el_input = newElement('input'); //create forest name input
      el_input.type = 'text';
      el_input.name = 'forest_text_' + iteration;
      el_input.id = 'forest_text_' + iteration;
      el_input.size = 40;
      frstNameCell.appendChild(el_input); 
  
      // Column which has the village name
      var villageNameCell = row.insertCell(2);
  
      var el_label = newElement('label');
      el_label.for = 'village_text_' + iteration + '_1';
      var el_labelValue = '1.';
      textNode = newTxt(el_labelValue);
      el_label.appendChild(textNode);
  
      villageNameCell.appendChild(el_label);
  
      el_input = newElement('input');
      el_input.type = 'text';
      el_input.name = 'village_text_' + iteration + '_1';
      el_input.id = 'village_text_' + iteration + '_1';
      el_input.size = 40;
      villageNameCell.appendChild(el_input);     
  
      el_btn = newElement('input'); //create village name add button
      el_btn.type = 'button';
      el_btn.name = 'village_btn_' + iteration;
      el_btn.id = 'village_btn_' + iteration;
      el_btn.value = 'Add Village Forest ' + iteration;
      el_btn.addEventListener('click',addMoreVillageNames, false);
  
      villageNameCell.appendChild(el_btn);
  
      el_btn = newElement('input'); //create village name remove button
      el_btn.type = 'button';
      el_btn.name = 'village_rembtn_' + iteration;
      el_btn.id = 'village_rembtn_' + iteration;
      el_btn.value = 'Remove Village Forest ' + iteration;
      el_btn.addEventListener('click',removeVillageName, false);
  
      villageNameCell.appendChild(el_btn);
  
  
      var frstManagCell = row.insertCell(3); // create forest management input
  
      el_input = newElement('input');
      el_input.type = 'text';
      el_input.name = 'frstManage_text_' + iteration + '_1';
      el_input.id = 'frstManage_text_' + iteration + '_1';
      el_input.size = 40;
      frstManagCell.appendChild(el_input);
  
  
      var yerPartCell = row.insertCell(4); // create year participatory input
  
      el_input = newElement('input');
      el_input.type = 'text';
      el_input.name = 'yrPart_text_' + iteration + '_1';
      el_input.id = 'yrPart_text_' + iteration + '_1';
      el_input.size = 40;
      yerPartCell.appendChild(el_input);  
  
  
      var frstSizeCell = row.insertCell(5); // create forest size input
  
      el_input = newElement('input');
      el_input.type = 'text';
      el_input.name = 'frstSize_text_' + iteration + '_1';
      el_input.id = 'frstSize_text_' + iteration + '_1';
      el_input.size = 40;
      frstSizeCell.appendChild(el_input);  
  
  
  }
  
  function addMoreVillageNames(){
      rowNumber = (this.id).substring((this.id.length)-1, this.id.length); //to get Row Number where one more input needs to be added.
  
      var childElCount;
      var parentCell = this.parentNode;
      var inputCount = parentCell.getElementsByTagName('label').length; //to get the count of input fields present already
      var newFieldNo = inputCount + 1; //input current count by 1 to dynamically set next number for the new field
  
      //temporarily remove the add and remove buttons to insert the new field before it.we are doing this loop only twice because we know the last 2 input elements are always the two buttons
      for (var i=0; i&lt;2; i++){
          childElCount = parentCell.getElementsByTagName('input').length; //get count of child input elements (including text boxes);
          parentCell.removeChild(parentCell.getElementsByTagName('input')[childElCount-1]);
      }
  
      var lineBreak = newElement('br');
      parentCell.appendChild(lineBreak); //add a line break after the first field
  
      var el_label = newElement('label');
      el_label.for = 'village_text_' + rowNumber + '_' + newFieldNo;
      var el_labelValue = newFieldNo + '.';
      var textNode = newTxt(el_labelValue);
      el_label.appendChild(textNode);
      parentCell.appendChild(el_label); //create and add label
  
      var el_input = newElement('input');
      el_input.type = 'text';
      el_input.name = 'village_text_' + rowNumber + '_' + newFieldNo;
      el_input.id = 'village_text_' + rowNumber + '_' + newFieldNo;
      el_input.size = 40;
      parentCell.appendChild(el_input); //create and add input field
  
      var el_btn = newElement('input'); //add the village name add button again
      el_btn.type = 'button';
      el_btn.name = 'village_btn_' + rowNumber;
      el_btn.id = 'village_btn_' + rowNumber;
      el_btn.value = 'Add Village ' + rowNumber;
      el_btn.addEventListener('click',addMoreVillageNames, false);
      parentCell.appendChild(el_btn);
  
      el_btn = newElement('input'); //create village name remove button
      el_btn.type = 'button';
      el_btn.name = 'village_rembtn_' + rowNumber;
      el_btn.id = 'village_rembtn_' + rowNumber;
      el_btn.value = 'Remove Village ' + rowNumber;
      el_btn.addEventListener('click',removeVillageName, false);
      parentCell.appendChild(el_btn);
  
  }
  
  
  function removeForestName()
  {
    var tbl = document.getElementById('tblSample');
    var lastRow = tbl.rows.length;
    if (lastRow &gt; 2) tbl.deleteRow(lastRow - 1);
  }
  
  function removeVillageName()
  {
      var rowNumber = (this.id).substring((this.id.length)-1, this.id.length); //to get Row Number where one more input needs to be added.
  
      var parentCell = this.parentNode;
      var lblCount = parentCell.getElementsByTagName('label').length;
      var inputCount = parentCell.getElementsByTagName('input').length;
      var brCount = parentCell.getElementsByTagName('br').length;
  
      //Remove will not happen if there is only one label-input combo left.
      if(lblCount&gt;1)
      parentCell.removeChild(parentCell.getElementsByTagName('label')[lblCount-1]);
  
      if(inputCount&gt;3)
      parentCell.removeChild(parentCell.getElementsByTagName('input')[inputCount-3]); //Delete the third last element because the last two are always the two buttons.
  
      parentCell.removeChild(parentCell.getElementsByTagName('br')[brCount-1]);
  }
  
  window.onload = addForestName;
  
  &lt;/script&gt;
  
  &lt;form action="tableaddrow_nw.html" method="get"&gt;
     &lt;table width="640" border="1" id="tblSample"&gt;
         &lt;tr&gt;
             &lt;td&gt;No.&lt;/td&gt;
             &lt;td&gt;Forest Name&lt;/td&gt;
             &lt;td width="40"&gt;Name of the villages &lt;br /&gt;participating in managing&lt;/td&gt;
             &lt;td&gt;Type of participatory forest management&lt;/td&gt;
             &lt;td&gt;Year participatory management process started&lt;/td&gt;
             &lt;td&gt;Size of forest&lt;/td&gt;
  
         &lt;/tr&gt;
    &lt;/table&gt;
  &lt;/form&gt;
  &lt;p&gt;
    &lt;input type="button" value="Add" onclick="addForestName();" /&gt;
    &lt;input type="button" value="Remove" onclick="removeForestName();" /&gt;
  &lt;/p&gt;
  </code></pre>