Any form elements created dynamically on the client-side will have no
intrinsic link to the ActionForm.  However, this is not necessarily a
problem... imagine if your ActionForm has this in it:

private String firstName;
public void setFirstName(String inFirstName) {
  firstName = inFirstName;
}
public String getFirstName() {
  return firstName;
}

What happens if your JSP *DOES NOT* include this form field?  Obviously
that will be just fine, Struts won't complain.

Now, what happens if you dynamically add that field to your HTML form via
JavaScript, and then submit the form?  Again, this will be just fine,
Struts will happily populate the firstName field in the ActionForm.  It
doesn't matter that it wasn't there when the HTML for the page was
originally rendered by the JSP.

In the case of indexed properties, the same is true... if you dynamically
add a field to the HTML form, so long as the name follows the index naming
paradigm, it will be populated in the ActionForm when submitted.

The code you have here looks basically correct, with one possible
exception... setting innerHTML doesn't necessarily add anything to the
DOM.  So, when the form is submitted, the fields you dynamically added may

not be sent (I believe it will work in some browsers, but not in others...
I'd have to go test to verify this, but that's what's in my memory).
Instead, you should use DOM methods to create your new field and append it

to the form, that should alleviate that problem.  This is generally the
preferred method to work with dynamic content anyway.  So, your add method
should be something like

function add() {
  var newField = document.createElement("input");
  newField.type = "text";
  newField.name = "foo[" + count + "]";
  var newCell = document.createElement("td");
  newCell.appendChild (newField);
  var newRow = document.createElement("tr");
  newRow.appendChild(newCell);
  var theTable = document.getElementById("t1");
  theTable.appendChild(newRow);
  count++;
}

Frank


ok this is actually fanatstic!!!!Thanks a tonne. But small problem, this
thing doesnt work on IE(and yes, like everyone, my target audience also has
it as there primary browser). I mean the rows dont get added to the page. It
stays static, doesnt throw any javascript errors either.. could you test it
out on your side??
I have yahoo toolbar on my IE. could that be causing the problem??
I have IE 6 and Firefox 1.5.0.6
It works easily on firefox.

--
Puneet

Reply via email to