You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Kruse, Matt" <MK...@aquent.com> on 2003/05/08 15:02:34 UTC

RE: Using JavaScript to reference Map or Indexed backed fields.

> <input type="text" name="myMap(fieldA)" />
> <input type="text" name="myField[0]" />
> but referencing either via;
> document.myForm.myMap(fieldA).value
> document.myForm.myField[0].value
> throws JS Object errors. How do I access these fields in one line?

Technically, both of those field names are invalid HTML. I'm surprised
Struts creates input fields with invalid HTML names.
According to the specs:
	ID and NAME tokens must begin with a letter ([A-Za-z]) and 
	may be followed by any number of letters, digits ([0-9]), 
	hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). 

PHP does the same kind of thing for array form variables, and it causes a
lot of javascript pain for people.

Josh was correct, though, in how to reference the fields. I often prefer to
declare my field names at the top of the page, using syntax like this:

var myfield1 = 'myField[0]';

Then in your javascript later, you can do:

document.myForm[myfield1].value="blah";

It often makes javascript code easier to read, avoids typos in field names,
and if your fields change names later, you just make the change in a single
place.

Matt