jQuery in Action, 3rd edition
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
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: 6319

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Javascript and Struts Indexed properties
It's not a secret anymore!