I ended up scratching my head today over a line of Javascript that was just not playing ball. The line was this:
opener.document.forms[0].plugin-media4pic.value = “avatar.jpg”;
All I was trying to do was update a field on the parent form that called a popup with an image that had been uploaded by the user. Sounds pretty straightforward, and I had used that type of code in the past when building the WP Auctions plugin. So I expected it to work right off the bat.
Instead, I kept getting an error saying “invalid assignment left-hand side”. I rechecked my code. I walked through the page DOM to ensure I wasn’t specifying something that didn’t exist. I rechecked my code. I went for a walk. I rechecked my code. I made some tea. I rechecked my code. I scratched my head for a bit. I rechecked my code. Nothing. I still kept getting the pesky error.
Eventually, out od desperation, I started trying different things, and found out what the problem was!
Turns out the problem was a form field called plugin-media4pic. For some inexplicable reason, Javascript interpreted the “-” as a minus sign and was trying to evaluate the left-hand side of the assignment expression above. Don’t ask me why, but changing the “-” to an “_” solved the problem. Can anyone explain why this was happening?
Hope you guys don’t run into something else inexplicable today.







Mike Cromwell on August 18th, 2008
1
I remember having a similar problem, I solved it by using the elements array of the Form object instead, so your example code would become:
opener.document.forms[0].elements['plugin-media4pic'].value = “avatar.jpg”;
hope this helps
Owen on August 19th, 2008
2
@Mike: Thanks for your comment. Yeah. That would have worked too. Just took me ages to figure out what the problem was!
Jesse Thompson on November 14th, 2008
3
That’s not so inexplicable, “-” just isn’t a valid character for identifiers in javascript. It’s reserved for uses like the minus sign.
Seriously, how else will Javascript know what you want when you talk about:
this.a-that.b
Are we accessing property b within propterty “a-that”, or are we subtracting this.b from this.a? So, the minus sign just can’t be used in bare identifiers.
It’s true that you can use form names that contain characters that don’t make valid JS identifiers, and in that case the bracket syntax saves the day.
As to why using a _ worked instead, there might be a provision in there somewhere which aliases abc-def and abc_def to mean the same thing. I for one would not count on such a provision however, as [] syntax clarifies your intentions unambiguously.
Mangia.
- - Jesse