I want to call a function when a button within an iframe is clicked, but I am already having problems accessing the iframe document.
First, I followed this video, but null is returned when the function is called. The iFrame's content is hosted on the same server, using a wordpress installation in a folder (mydomain.com/fragen). The main page is mydomain.com, so that should not be the issue, right? Here is the code I was using. Printing a string to the console works.
<button onclick="test()">Test</button>
<iframe id="myIframe" src="mydomain.com/fragen/wp-admin/admin-ajax.php?action=h5p_embed&id=24" width="100%" height="525" frameborder="0" allowfullscreen="allowfullscreen" title="Diagnosetext">
</iframe>
<script>
const myIframe = document.querySelector("#myIframe");
function test() {
const iframeWindow = myIframe.contentWindow;
console.log(iframeWindow);
}
</script>