wood burning stoves 2.0*
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
Author

Javascript and Struts Indexed properties

Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
Hello,
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?
Thanks


Commentary From the Sidelines of history
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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.
HTH
SR
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Javascript and Struts Indexed properties