954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

setting dynamically created values to Formbean

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 = "";
oCell.style.verticalAlign = "top";
id++;
}

My table design is like this

Field Name
+

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

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 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......

rp_prasanna
Newbie Poster
14 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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;
}
}
mamatachaudhari
Newbie Poster
20 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You