# Accessing the iFrame elements.



## ali_ggl (Feb 15, 2008)

I am trying to access the content of my iframe src page. Something
like below

```
document.getElementById('myFrame').contentWindow.document;
```
It works fine on a predifined iframe element, However, when creating
the iframe element
dynamically, and appending it to the body, I can't access the content
when doing this dynamic insertion. My guess would be that this is
because the iframe element doesn't yet exist on the Dom. See example
of problem below:


```
var myFrame = document.createElement("iframe");
myFrame.id = "testID";
myFrame.name = "testID";
myFrame.src = "testPage.htm";
myFrame.frameBorder = "0";

document.body.appendChild(myFrame);

//code below isn't working due to the above
document.getElementById('testID').contentWindow.document;
```
Does anyone have any example to access the elements of iFrame or Frame in browser
so I can do to resolve this issue?

Thanks 
Ali


----------



## tomdkat (May 6, 2006)

ali_ggl said:


> Does anyone have any example to access the elements of iFrame or Frame in browser
> so I can do to resolve this issue?


A better question is does anyone have an example of accessing elements of a dynamically created iframe? 

I've access elements in an iframe using the method that also worked for you BUT it was also with an existing iframe, as was in the case that worked for you.

One question I have is why are you creating the iframe dynamically? If you don't want the iframe to exist until some event happens or under some other condition(s), why not put the iframe in a DIV that isn't displayed until you want the iframe to be displayed? You can get sample HTML that hides/shows DIVs here. That example is using a web form but hiding/displaying of the DIVs is the part that is relevant.

Peace...


----------

