最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

forms - Hyphen in div id causing javascript error - Stack Overflow

matteradmin3PV0评论

I'm having an issue with javascript whereby i am performing the following to close a popup window and update a field in the parent window with the required value. Code looks something like this:

<script language="javascript" type="text/javascript">
    var FieldID = document.form.field22-1.value;
    self.parent.opener.document.+FieldID = 'some text';
    window.top.window.close();
</script>

However I am getting the following error:

Error: missing ; before statement

I have a funny feeling the javascript is interpreting the field id (field22-1) as having a subtraction in it. Which I guess would make sense. Any ideas/help would be ridiculously appreciated, really don't want to have to go back in and change the - in the code!

Thanks in advance!

I'm having an issue with javascript whereby i am performing the following to close a popup window and update a field in the parent window with the required value. Code looks something like this:

<script language="javascript" type="text/javascript">
    var FieldID = document.form.field22-1.value;
    self.parent.opener.document.+FieldID = 'some text';
    window.top.window.close();
</script>

However I am getting the following error:

Error: missing ; before statement

I have a funny feeling the javascript is interpreting the field id (field22-1) as having a subtraction in it. Which I guess would make sense. Any ideas/help would be ridiculously appreciated, really don't want to have to go back in and change the - in the code!

Thanks in advance!

Share asked Jan 19, 2011 at 18:06 richrich 1,2251 gold badge20 silver badges36 bronze badges 1
  • Never try to get elements directly. It just causes problems (as you can see) – Dustin Davis Commented Jan 19, 2011 at 18:09
Add a ment  | 

4 Answers 4

Reset to default 6

Use document.getElementById('field22-1').value instead.

You might also need to fix this:

self.parent.opener.document[FieldID] = 'some text';

In JavaScript, any property of any object can be accessed either via dot notation, e.g. foo.bar, or bracket notation, e.g. foo["bar"]. The latter is necessary when your property is not a legal identifier (as in your case):

var FieldID = document.form["field22-1"].value;

Alternatively, if this is an actual id attribute, you should use:

var FieldID = document.getElementById('field22-1').value;

You could also use document.form['field22-1'].value.

You can use document.getElementById('field22-1').value

Post a comment

comment list (0)

  1. No comments so far