Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javascript and Struts Indexed properties

 
Paul McKenna
Ugly Redneck
Ranch Hand
Posts: 1006
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);" />
 
Paul McKenna
Ugly Redneck
Ranch Hand
Posts: 1006
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic