i have a Struts jsp which creates a textbox dynamically using javascript.

function addRow_msgField()
{           
    var id =0;
    var table = document.getElementById("msgfield");
    var id = table.rows.length-1;  
var newRow = document.all("msgfield").insertRow();          
var oCell = newRow.insertCell();                
oCell.innerHTML = "<input type='text' name=alertFieldName"+id+"  class='textBoxStyle' size='12' maxlength='50'/>";
oCell.style.verticalAlign = "top";
id++;
}

My table design is like this

<table align="center" id="msgfield" width="100%" cellpadding="1" cellspacing="0" border="1"   bordercolorlight="#999999" bordercolordark="#FFFFFF" bordercolor="black">
<tr class="tableDataTextStyle">
<td width="15%">Field Name </td>
<td width="5%" align="center" onclick="addRow_msgField();">+</td>
</tr>
</table>

Say that three text box is created dynamically clicking the + sign.

<html:form action="/apialert" >
</html:form>

The FormBean for this action class is ApiAlertBean which contains

private String[]  fieldname;

When i click submit button the values in each textbox should be set in the feilname array String of the FormBean.

I tried using <logic:iterate> tag but could not acheive it since the no.of textboxes is dynamic (i.e it can be one or more )

Then i tried using hidden variable, i.e in js i append all the textbox values using deliminator (,) and then set the value to the hidden variable. i didnot succed in that also......

You can declare id variable as global in init() method as call it onload of body tag

function init()
{
   id =0;
}

then append id to text box id property which u dynamically created.

Declare a combo Box of name fileNames. as

<html:select property="fileNames" multiple="true">
<html:option value="">-Select-</html>
</html:select>

when You submit form you can use

function submit(form)
{
  var newElem="";
      for(int j=0;j<id;j++)
    {
      newElement =document.createElement("<OPTION>");
      var textBox1=document.getElementById("alertFieldName"+id);
      newElement.text=textBox1.value;
      newElement.value=textBox1.value;
    form.fileNames.options.add(newElement);
     }

for(var i=0;i< form.fileNames.options.length;i++)
{
  form.fileNames.options.selected=true;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.