title: Creating struts 2 forms dynamically on jsp using java script
body: <p>What I require is a pretty standard feature. And I am sure its easy enough, but somehow I cant make it happen. Please help me out here.</p>
  
  <p>This is the scenario--></p>
  
  <p>I have a struts form on a jsp, which takes in employee information. Now with every employee I want to associate some family members.</p>
  
  <p>So for information of family members I want :</p>
  
  <p>1.) A row of -- 1 select element and 3 text field elements -- in the end of the form. </p>
  
  <p>2.) A 'add' button which appends such rows on demand for adding more family members.</p>
  
  <p>I dont know how I can attach a screen shot to give you exact idea of what I want.</p>
  
  <p>I have tried doing this, using javascript, but javascript adds standard HTML elements, because of which I am not able to access the value of those fields in my action class.(Please tell me if this is not the case, because then the only question that will remain is, why am I unable access those values)</p>
  
  <p>Currently what I am trying:</p>
  
  <p>JSP:</p>
  
  <pre><code>&lt;s:form name="enterEmployeeInfo" id="enterEmployeeInfo" action="enterEmployeeInfo"&gt;
  
      ////OTHER FORM ELEMENTS//////////////
  
          &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Relationship&lt;/td&gt;
              &lt;td&gt;Name&lt;/td&gt;
              &lt;td&gt;Age&lt;/td&gt;
              &lt;td&gt;Occupation&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;select name="rel"&gt;
                      &lt;option value=""&gt;&lt;/option&gt;
                      &lt;option value="Father"&gt;Father&lt;/option&gt;
                      &lt;option value="Mother"&gt;Mother&lt;/option&gt;
                      &lt;option value="Spouse"&gt;Spouse&lt;/option&gt;
                      &lt;option value="Child"&gt;Child&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/td&gt;
              &lt;td&gt; &lt;input name="rName[]"/&gt;&lt;/td&gt;
              &lt;td&gt; &lt;input name="rAge"/&gt;          &lt;/td&gt;
              &lt;td&gt; &lt;input name="rOccupation"/&gt;   &lt;/td&gt;
              &lt;td colspan="4" align="right"&gt;&lt;button type="button" onclick="tryFunc(this.parentNode);"&gt;Add&lt;/button&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;/table&gt;
          &lt;s:submit value="Add Employee" name="submit"/&gt;
          &lt;s:reset  value="Reset"       name="reset"/&gt;
  &lt;/s:form&gt;
  </code></pre>
  
  <p>The JS:</p>
  
  <pre><code>function tryFunc(node){
      var root = node.parentNode.parentNode;
      var allRows = root.getElementsByTagName('tr');
      var cRow = allRows[1].cloneNode(true);
      var cInp = cRow.getElementsByTagName('input');
      for(var i=0;i&lt;cInp.length;i++){
          cInp[i].setAttribute('name',cInp[0].getAttribute('name')+'_'+(allRows.length+1))
      }
      var cSel = cRow.getElementsByTagName('select')[0];
      cSel.setAttribute('name',cSel.getAttribute('name')+'_'+(allRows.length+1));
      root.appendChild(cRow);
  }
  </code></pre>
  
  <p>With this I am able to add a new row of specified elements, but unable to access the field values in the action class. I would like to point out that I am not able to access even the first row's elements in action class (probably because they are standard HTML).</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Thanks!!</p>