I have a page which contains an iframe to another page, what I am looking for is to click something inside the iframe, which can then access or more specifically hide a div in the parent. I know some JQuery but for some reason just sit there blankly at the code editor when I try to write it on my own
To access elements of parent window from iframe with JavaScript, we can use the window. parent. document property to get the parent window's document.
The window will get opened by using the iframe, and the communication could be done with the parent window from the child window. To call a parent window function, use the following syntax and also refer to the code given below.
Getting the element in IframegetElementById() method by passing iframe id as an argument. const iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe.
This method safely enables cross-origin communication. And if you have access to parent page code then any parent method can be called as well as any data can be passed directly from Iframe.
try this in iframe:
$('#DIVtobehidden', window.parent.document).hide();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With