How to get elements from within an iFrame with JavaScript?

Spread the love

Sometimes, we want to get elements from within an iFrame with JavaScript.

In this article, we’ll look at how to get elements from within an iFrame with JavaScript.

How to get elements from within an iFrame with JavaScript?

To get elements from within an iFrame with JavaScript, we use the contentDocument property.

For instance, we write

const iframe = document.getElementById("iframeId");
const innerDoc = iframe.contentDocument ?? iframe.contentWindow.document;

to select the iframe with getElementById.

Then we get the iframe’s document object with contentDocument or contentDocument.document.

Conclusion

To get elements from within an iFrame with JavaScript, we use the contentDocument property.

Leave a Reply

Your email address will not be published.