323

How do you get a <div> from within an <iframe>?

asked Jul 6, 2009 at 18:31
0

7 Answers 7

493
var iframe = document.getElementById('iframeId');
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;

You could more simply write:

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;

and the first valid inner doc will be returned.

Once you get the inner doc, you can just access its internals the same way as you would access any element on your current page. (innerDoc.getElementById...etc.)

IMPORTANT: Make sure that the iframe is on the same domain, otherwise you can't get access to its internals. That would be cross-site scripting. Reference:

starball
59.3k52 gold badges312 silver badges1k bronze badges
answered Jul 6, 2009 at 18:35
Sign up to request clarification or add additional context in comments.

14 Comments

Great answer. Did you know that you can do: var innerDoc = iframe.contentDocument || iframe.contentWindow.document; //more readable and means the same
I've seen people confused by that more than ternary operators. They seem to not know that the first valid one is returned.
in fact, as i edited the answer to include it, the guy over my shoulder asked me what that did...
Probably better to educate than use more complex code for simplicity :)
Is there a way to get access to the iframe internals if the iframe originates from a different URL. You mentioned that it is cross-site scripting, but I am wondering if there is a way.
|
23

Do not forget to access iframe after it is loaded. Old but reliable way without jQuery:

<iframe src="samedomain.com/page.htm" id="iframe" onload="access()"></iframe>
<script>
function access() {
 var iframe = document.getElementById("iframe");
 var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
 console.log(innerDoc.body);
}
</script>
answered Mar 14, 2017 at 9:44

Comments

12

Above answers gave good solutions using Javscript. Here is a simple jQuery solution:

$('#iframeId').contents().find('div')

The trick here is jQuery's .contents() method, unlike .children() which can only get HTML elements, .contents() can get both text nodes and HTML elements. That's why one can get document contents of an iframe by using it.

Further reading about jQuery .contents(): .contents()

Note that the iframe and page have to be on the same domain.

answered Apr 19, 2018 at 7:44

Comments

7
window.parent.document.getElementById("framekit").contentWindow.CallYourFunction('pass your value')

CallYourFunction() is function inside page and that function action on it

Roko C. Buljan
209k41 gold badges335 silver badges346 bronze badges
answered Sep 19, 2014 at 12:36

1 Comment

That's calling a function from an iFrame, not getting a reference to the element.
1

None of the other answers were working for me. I ended up creating a function within my iframe that returns the object I was looking for:

function getElementWithinIframe() {
 return document.getElementById('copy-sheet-form');
}

Then you call that function like so to retrieve the element:

var el = document.getElementById("iframeId").contentWindow.functionNameToCall();
answered Aug 14, 2015 at 15:30

Comments

0

If iframe is not in the same domain such that you cannot get access to its internals from the parent but you can modify the source code of the iframe then you can modify the page displayed by the iframe to send messages to the parent window, which allows you to share information between the pages. Some sources:

answered Mar 20, 2019 at 21:22

Comments

0

You can use this function to query for any element on the page, regardless of if it is nested inside of an iframe (or many iframes):

function querySelectorAllInIframes(selector) {
 let elements = [];
 const recurse = (contentWindow = window) => {
 const iframes = contentWindow.document.body.querySelectorAll('iframe');
 iframes.forEach(iframe => recurse(iframe.contentWindow));
 
 elements = elements.concat(contentWindow.document.body.querySelectorAll(selector));
 }
 recurse();
 return elements;
};
querySelectorAllInIframes('#elementToBeFound');

Note: Keep in mind that each of the iframes on the page will need to be of the same-origin, or this function will throw an error.

answered Nov 23, 2020 at 21:44

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.