Yeah, leave it to IE to not work with DOM-compliant code :) I guess I
only tried it in FF, which is odd since I don't generally use FF (it was
probably just open at the time!). Glad you got it working!
Frank
Puneet Lakhina wrote:
> On 8/3/06, Puneet Lakhina <puneet.lakhina@gmail.com> wrote:
>>
>>
>>
>>
>> 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
>>
>
> ok.. got it to work on IE too. Some javascript trial and error.
> This thing works, why and how the earlier thing doesnt work on IE will take
> time to figure out
>
> tha javascript function
> count=1;//one cell is already there using
> function add() {
> var theTable = document.getElementById("t1");
> var row = theTable.insertRow(count);
> var newField = document.createElement("input");
> newField.type = "text";
> newField.name = "list[" + count + "]";
> var newCell = row.insertCell(0);/*you could have some kind of count here if
> u want multiple cells*/
> newCell.appendChild(newField);
> count++;
> }
>
--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: fzammetti@hotmail.com
Author of "Practical Ajax Projects With Java Technology"
(2006, Apress, ISBN 1-59059-695-1)
Java Web Parts - http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
|