Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Error, Missing Name After . Operator

Tags:

javascript

I am trying to get the innerHTML of a hidden span. The JavaScript is from an iframe HTML page, and the hidden span resides in the parent page. A different function works when accessing contents of a list from the parent, but I can't seem to get at my span...

WORKS

document.getElementById(parent.genL[i]);

DOESNT WORK

document.getElementById(parent."span"+i).innerHTML;
- SyntaxError: missing name after . operator

The above line of code resides in a for loop and as it iterates through i it will grab data from each separate span. the hidden spans start at ID "span1" through upwards of 10-40k different hidden spans.

Anyways, I have a feeling that it has to do something with trying to concatenate the string int i. I assume i is an int anyways. Any thoughts? Thanks so much everyone!

Edit - Words, and added the innerHTML portion to the doesn't work line of code. Not sure if that will be making a difference or not...

Edit2 - Great answers everyone, learned some good syntactical tricks :) I simply moved the parent. portion to the front of the code as reccomend by the comment of mplungjan and the answer from Jacob T. Nielson. For some reason I still got the error using the brackets as suggested, but I will definitely tuck the brackets into my memory for future similar situations!

parent.document.getElementById("span"+i).innerHTML;

:)

like image 902
0xhughes Avatar asked Apr 19 '13 14:04

0xhughes


3 Answers

Try changing it to an indexer.

document.getElementById(parent["span"+i]);
like image 86
Daniel A. White Avatar answered Oct 02 '22 19:10

Daniel A. White


If the parent in the brackets is an object and you're trying to access something like parent.span1 then you need to use bracket notation instead of the dot.

document.getElementById(parent["span"+i]); should work fine.

like image 22
Trolleymusic Avatar answered Oct 02 '22 20:10

Trolleymusic


I think what you are trying to do is get the i-th span element on the parent page. Correct?

You can do it like this

var s = parent.document.getElementsByTagName('span')[i];
s.innerHTML // <-- access innerHTML
like image 32
Jacob T. Nielsen Avatar answered Oct 02 '22 20:10

Jacob T. Nielsen