Forum Moderators: open

Message Too Old, No Replies

referencing an object with special characters in it

myForm.'name[0]'.value escaping array caracters

         

phazei

3:21 am on Aug 21, 2007 (gmt 0)

10+ Year Member



This is kind of a cross between php and javascript question, but it's more to do with the javascript.

When having related checkboxes or fields and sending a form to a php script sometimes it's really usefull to make the names,
name="choice[1]" "choice[2]" "choice[3]" etc so it gets posted to the php as an array.

When dealing with these names in javascript before submission, with perhaps a js validation script it's a hassle to reference these variables.

If I already have a form Obj, say myForm and I go:
myForm.choice[1].value it fails as it reads choice as an array instead of variable name "choice[1]".
So I always have to go:
choice1 = document.getElementById('choice[1]');
choice1.value

I was just wondering if there was a way I could skip that step, and use something like...
myForm.{choice[1]}.value
Just so it doesn't read the [] as special caracters used for an array. Because if there are lots of [] that each need to be checked it could be a pain.

Thanks
adam

daveVk

4:41 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try

myForm['choice[1]']

phazei

7:16 am on Aug 24, 2007 (gmt 0)

10+ Year Member



wow, that was really easy. :)

thanks!