How to interact to iframe content using javascript?

Hello, i have found this example that show how javascript can interact with an iframe html, but i’m not able to make it work, will keep trying, but if someone can help is welcome :smile:

Seems i have found how to make it work, but i have an error ‘access denied’ i have to understand if it is because the page doesn’t load before the following code is launched or because the host of the site doesn’t allow external access

        var ifrm = document.getElementById('myframe');
        var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
        var inventory = doc.getElementById('inventory');
        //var inventory =  document.getElementById('myframe').contentWindow.document.getElementById('inventory');
        if (inventory) {
            inventory.addEventListener('click', function (e) {
                alert("pressed");
            }, true);
        }

Have you read this thread: