Categories
JavaScript Answers

How to insert content into iframe with JavaScript?

Spread the love

Sometimes, we want to insert content into iframe with JavaScript.

In this article, we’ll look at how to insert content into iframe with JavaScript.

How to insert content into iframe with JavaScript?

To insert content into iframe with JavaScript, we use the write method.

For instance, we write

const doc = document.getElementById("iframe").contentWindow.document;
doc.open();
doc.write("Test");
doc.close();

to select the iframe with getElementById.

Then we get its document object with contentWindow.document.

Next, we call open to open the document.

We call write to write the content we want into the iframe’s document.

And then we call close to finish writing.

Conclusion

To insert content into iframe with JavaScript, we use the write method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.