Accessing form elements


#1

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?


#2

If you’re using prototype, you can use

$(‘model_field’).value

-Thomas

Am 08.03.2006 um 21:19 schrieb rmb:


#3

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


#4

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