[thelist] Problem accessing for values with []

Jonathan Dillon jdillon at boehm-ritter.com
Thu Feb 10 17:36:16 CST 2005


> >I'm on to my second odd problem!  I've got a javascript file
> that needs
> >to make reference to the following form: 
> >document.myform.form[fieldname].value
> >
> >But:
> >alert('val: ' + document.main.form[journal].value);

> Should that not be:
>
>alert('val: ' + document.main.form['journal'].value);

Thanks to Chris and everyone else for the replies.  I guess I should have
been more clear.  form[fieldname] wasn't an attempt to access a form
element, but it's actual name="" element of the form (the whole thing,
including the form[]).  Sometimes, don't you just WISH you posted the html
too? 

I was actually able to solve it with:
document.main['form[journal]'].value

This escapes it as a literal value and seems to work ok.  Using Christian's
technique, just for the archive's sake:

document.getElementById('journal')

Would be

document.getElementById('form[journal]')

Looks weird.

Thanks again!

Jonathan



More information about the thelist mailing list