14

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

4
  • 1
    Are the page and the iframe hosted on the same subdomain?
    – rekire
    Jul 30, 2012 at 8:34
  • yes they are, I have access to both pages if the code needs to go in either Jul 30, 2012 at 8:34
  • @Andrew Morris The code acces is not the problem – but browsers do not allow JavaScript actions across different domains. And those domains must match exactly (even the protocol must be the same – you can't handle https site via JS from an http site)
    – feeela
    Jul 30, 2012 at 8:43
  • @feeela, I read about the same domain policy and I knew the pages wouldn't be restricted by this, I just forgot to mention it in the question. Thanks anyway though Jul 30, 2012 at 8:53

3 Answers 3

34

try this in iframe:

$('#DIVtobehidden', window.parent.document).hide();
2
  • will this work if the parent is on a different domain?
    – Awalias
    Apr 24, 2014 at 17:32
  • This answer helped me solve a similar problem I was having, adding custom enhancements to an old 3rd-party web application - but it was not the complete solution. In my case I needed to call .trigger('someevent') on the element in the parent frame, and it just wasn't working - there appeared to be no handler for the event in the JQuery data structures. Instead, I had to resort to the rather hacky-looking construct: window.parent.$("myselector").trigger("someevent") which despite being ugly, at least has the benefit of working!
    – Loophole
    Nov 11, 2020 at 6:32
2
$("#element-In-Iframe").on('click', function() {
    $('#element-in-parent-window', window.parent.document).hide();
});

FIDDLE

1
  • @CrystalMiller - It used to work three years ago, note that it's also exactly the same as the accepted answer. These days JSFiddle is running it's own iframes from a shell subdomain, so the same-origin policy kicks in.
    – adeneo
    Jul 8, 2015 at 1:34
0

If both pages are on the same (sub)domain you should be able to access the parent window with:

window.parent.document.getElementById('divId')

Well this is without jQuery but should work

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.