Thursday, January 1, 2009

Javascript and complex field names

If your web form has need for a more complex naming structures for the field names like "user.first_name" instead of the basic "firstName". Then you might have run into issues trying to access these fields for a javascript form validation.

For the basic field name like "firstName":
<input type="text" name="firstName" value="">

You could access this by using the following:
_form.firstName.value

But if you are using the more complex naming structure like "user.first_name":
<input type="text" name="user.first_name" value="">

The _form.user.first_name.value will not work. You need to access it like this:
_form["user.first_name"].value

For more consistent style of code when using complex field names you should go ahead use the style _form["blah"].value for all of the elements in your javascript.

Have a great Day!

No comments: