9

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;

:)

3 Answers 3

6

Try changing it to an indexer.

document.getElementById(parent["span"+i]);
Sign up to request clarification or add additional context in comments.

2 Comments

They're both "indexing". The difference in terms is "dot notation" (what the OP is trying) vs. "bracket notation" (what you have)
Or perhaps parent.document.getElementById("span"+i);
6

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.

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.