开发者

Hyphen in div id causing javascript error

I'm having an issue with javascript whereby i am performing the following to close开发者_如何学Go 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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜