Control iframe content with javascript/html

Yes you can. Say your iframe‘s ID is 'myIFrame'

<iframe id="myIFrame" src="https://stackoverflow.com/questions/19498725/thispage.html"
    width="100%" height="600"
    frameBorder="2">
</iframe>

Then, add the following in your JavaScript:

// Get the iframe
const iFrame = document.getElementById('myIFrame');

// Let's say that you want to access a button with the ID `'myButton'`,
// you can access via the following code:
const buttonInIFrame = iFrame.contentWindow.document.getElementById('myButton');

// If you need to call a function in the iframe, you can call it as follows:
iFrame.contentWindow.yourFunction();

Hope it helps 🙂

Leave a Comment