Forum: Ruby on Rails accessing form elements

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
rmb (Guest)
on 2006-03-08 22:19
Rails names its form fields as model[field] , but you can't access the
value in javascript by document.form.model[field].value.  How do you
escape the ['s to access the value?
Thomas F. (Guest)
on 2006-03-08 22:29
(Received via mailing list)
If you're using prototype, you can use

$('model_field').value

-Thomas

Am 08.03.2006 um 21:19 schrieb rmb:
Brian V. Hughes (Guest)
on 2006-03-08 22:48
(Received via mailing list)
rmb wrote:
> Rails names its form fields as model[field] , but you can't access the
> value in javascript by document.form.model[field].value.  How do you
> escape the ['s to access the value?

I haven't tested this, but I believe you would do:

   document.form['model[field]'].value

The other thing you could do is use the form field's ID value, with a
getElementById DOM call:

   element = getElementById('model_field')

This assumes, of course, that you have unique IDs for all your form
elements.

-Brian
rmb (Guest)
on 2006-03-08 23:01
Brian V. Hughes wrote:
> rmb wrote:
>> Rails names its form fields as model[field] , but you can't access the
>> value in javascript by document.form.model[field].value.  How do you
>> escape the ['s to access the value?
>
> I haven't tested this, but I believe you would do:
>
>    document.form['model[field]'].value
>
> The other thing you could do is use the form field's ID value, with a
> getElementById DOM call:
>
>    element = getElementById('model_field')
>
> This assumes, of course, that you have unique IDs for all your form
> elements.
>
> -Brian

The second one works.  The first one gives a javascript "has no
properties" error
This topic is locked and can not be replied to.