Categories
JavaScript Answers

How to pick element inside iframe using document.getElementById with JavaScript?

Spread the love

Sometimes, we want to pick element inside iframe using document.getElementById with JavaScript.

In this article, we’ll look at how to pick element inside iframe using document.getElementById with JavaScript.

How to pick element inside iframe using document.getElementById with JavaScript?

To pick element inside iframe using document.getElementById with JavaScript, we can use the contentWindow property.

For instance, we write

const el = document
  .getElementById("myframe1")
  .contentWindow.document.getElementById("x");

to select the iframe with getElementById.

Then we use contentWindow.document.getElementById to select the element with ID x in the iframe.

Conclusion

To pick element inside iframe using document.getElementById with JavaScript, we can use the contentWindow property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *