Randall Jamison
Randall Jamison

Reputation: 329

Javascript/jQuery - how do I get a parent element from within multiple iframes?

I'm working within a setup that I have no control over:

Parent->Iframe->Iframe->My document

How do I access an element that is on the parent from within my document?

These are all on the same domain, so no cross-domain issues. I can do this with either straight up JS or jQuery.

I've been searching around, but haven't found any examples of someone trying to access an element on the top from the bottom through multiple iframes!

The solution, in case anyone else comes across this:

var p = $("#Viewport",window.top.document); 
alert(p.attr('name'));

Upvotes: 1

Views: 315

Answers (2)

StackSlave
StackSlave

Reputation: 10627

Just use window.top, like:

console.log(top.document.getElementById('someInputId').value);

Notice that window in implicit, so you can leave it off. Of course, you will have to change 'someInputId' to an input id on your top page to see if this works. Use .innerHTML instead of .value if you are testing against an Element that is not an input.

Upvotes: 1

MW5280
MW5280

Reputation: 16

I believe you will be able to access this via window.parent, as thus:

window.parent.document.getElementById('target-element');

Upvotes: 0

Related Questions