"form_for" names fields using brackets = javascript problems

Hi all,

The “form_for” helper and its friends “text_field_for” etc generate form
fields with names like "forum[title]”, i.e. using square brackets to
denote attributes on model objects.

Now, for the convenience of the user I want to do some simple
client-side validation to check that some fields have been filled in. So
I am trying to write javascript like

“if (myForm.forum[title].value == ‘’) { return false; }”,

but ofcourse the “title” in the brackets is interpreted as the name of
a child object of a “forum” object in the DOM for the page. Doh!

Is there a way to solve this?

I realize this is more a javascript question than RoR question, but I
have looked fruitlessly for an answer and I figure people trying
client-side validation in RoR must have stumbled on this problem.

Please note that I am NOT using client-side validation to replace
server-side, it is just used as an added benefit for javascript enabled
clients.

Thanks,

/Andreas

The “form_for” helper and its friends “text_field_for” etc generate form
fields with names like “forum[title]”, i.e. using square brackets to
denote attributes on model objects.

Now, for the convenience of the user I want to do some simple
client-side validation to check that some fields have been filled in. So
I am trying to write javascript like

“if (myForm.forum[title].value == ‘’) { return false; }”,

myForm.elements[‘forum[title]’].value

-philip

Philip H. wrote:

The “form_for” helper and its friends “text_field_for” etc generate form
fields with names like “forum[title]”, i.e. using square brackets to
denote attributes on model objects.

Now, for the convenience of the user I want to do some simple
client-side validation to check that some fields have been filled in. So
I am trying to write javascript like

“if (myForm.forum[title].value == ‘’) { return false; }”,

myForm.elements[‘forum[title]’].value

-philip

Thanks philip! I’ll try it out later today, seems like it will get the
job done!

/Andreas