Can anyone fix my bad Javascript?
November 25, 2006 4:22 PM   RSS feed for this thread Subscribe

I have to set the content of a field using Javascript, but the field's name has a dot in it, making Javascript treat it like a delimiter. Maybe I'm just tired. Help me escape?

I seem to have left my books at home and I'm in an airport trying to fix something. I have to set the values (contents) of some fields that have names containing dots. This makes Javascript unhappy.

   

   


But this produces the expected "document.editForm.class2 has no properties" error. I have tried XML-style escaping the dot that matters, and also using square bracket style notation...

   

   



   

   


But then I get "missing name after . operator" (Javascript doesnt like the dot before 'value', apparently.)

I think I'm out of things to try, and my Googling leads me to examples like the above that allegedly work. I'm stuck using the field-names with dots, because it's feeding an app I can't control. If I remove the dots and name the field "arbitraryFieldName", it all works, so the dot is definitely the problem.

Can anyone see what I'm missing? I'm prepared to feel dumb. :)
posted by rokusan to computers & internet (8 comments total)
Grrr, the post ate my fake html. The code snip in question looked like this:


{input type="text" name="class2.repliedDate" value="11/01/06"}

{input type="button" value="TEST" onClick="document.editForm.[class2.repliedDate].value = '11/25/06'"}


With regular angle-brackets instead of curlies.
posted by rokusan at 4:25 PM on November 25, 2006


Maybe:

document.editForm.['class2.repliedDate'].value
posted by null terminated at 4:40 PM on November 25, 2006


you don't say if it is javascript for IE, Firefox, or whatever. It may make a difference. If it is Firefox/mozilla I can help.

Also I can't see how much of your code got eaten.

Using something like form1.name2 is really an 'expando-property'. The non-shortcut way to access such is form1.elements.namedItem('name2').
posted by MonkeySaltedNuts at 4:53 PM on November 25, 2006


document.editForm.['class2.repliedDate'].value

This is essentially correct, but omit the . before the opening square bracket. (FireFox, at least, doesn't like it.)

(One of the nice things about Javascript is that objects are basically a special case of hash tables; the foo['bar'] hash key syntax and foo.bar property accessor syntax are generally interchangable.)
posted by IshmaelGraves at 5:05 PM on November 25, 2006


Both f1.elements.namedItem('class2.repliedDate') and f1['class2.repliedDate'] work in the Javascript Shell. (at least with Firefox).
posted by MonkeySaltedNuts at 5:13 PM on November 25, 2006


Yeah, IshmaelGraves is right...I missed the extra dot.
posted by null terminated at 6:07 PM on November 25, 2006


Thanks. The blue (green?) comes through again. Best use of an airport lounge ever. Either of these worked, indeed:

document.editForm['class2.repliedDate'].value
document.editForm.elements.namedItem('class2.repliedDate').value


I have never used the latter form, but I should have guessed what was wrong with the fromer.

Thanks for the eyes, kids. Mine were bleeding.
posted by rokusan at 6:48 PM on November 25, 2006


*former. :)
posted by rokusan at 6:49 PM on November 25, 2006


« Older Timer and Control Circuit. I ...   |   I am so screwed. I committed t... Newer »
This thread is closed to new comments.