This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Javascript and Struts Indexed properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Javascript and Struts Indexed properties" Watch "Javascript and Struts Indexed properties" New topic

Javascript and Struts Indexed properties

Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
I have a HTML form that uses Struts indexed properties and hence looks kinda like this

When I use javascript to validate these fields the javascript fails because javascript cannot recognise "myprop[0]" as an element name. Any suggestions or work around?

Commentary From the Sidelines of history
Junilu Lacar

Joined: Feb 26, 2001
Posts: 4442

Can you refactor so that you are calling JavaScript from an event in the form element itself? If you can, you can pass "this", e.g.
<html:text property="myprop[0]" onchange="javascript:foo(this);" />

Junilu - [How to Ask Questions] [How to Answer Questions]
Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
Thanks! I will try out that approach too. I found a way around though
Lets assume my html code looks like the following :

In the javascript function we can still validate these boxes by using the following script:

So basically the documents.myform.elements return an array with all the form elements. You have to remember that you will get everything within the form from labels to textboxes to button etc. So you have to use multiple if statements to check if the name of the element corresponds to something you want to validate and then perform the required validation.
jQuery in Action, 2nd edition
subject: Javascript and Struts Indexed properties
Similar Threads
How I can edit 'java.util.Set' DynaValidatorForm form-property
Submit form text fields (group) with same name
how to do submit the Dynamic produced element in Struts
Retrieving strings using getStrings() - DynaValidatorActionForm
DynaActionForms (struts 1.2) - form-property type