user188962
user188962

Reputation:

pass variable to child in javascript?

I want to pass a variable in my parent window to an iframe html document (child).

Example: var myValue = 10; want to pass myValue to a child element (hidden input)...

How would I do this?

Upvotes: 0

Views: 3047

Answers (5)

Brandon G
Brandon G

Reputation: 2893

I'm thinking this should work. May need to be tweaked a bit.

var myIframe = document.getElementById('iFrameId');

if ( myIframe.contentDocument ) { // DOM
    myIframe.contentDocument.getElementById('hiddenField').value = myValue;

} else if ( myIframe.contentWindow ) { // IE win
    myIframe.contentWindow.document.getElementById('hiddenField').value = myValue;
}

Upvotes: 0

Pops
Pops

Reputation: 30828

Following on to Steffen's answer, I did something like this earlier today.

if(document.getElementById("foo").document) { // IE
    searchableContent = document.getElementById("foo").contentWindow.document;
}
if(document.getElementById("foo").contentDocument) { // Firefox, Chrome
    searchableContent = document.getElementById("foo").contentDocument;
}

Object detection might be tangentially of interest to you.

Upvotes: 1

Murali VP
Murali VP

Reputation: 6417

From the parent's context: window.frames contains the frame you want. If the frame is in the same domain, use getElementById/getElementsByName to locate your hidden input and assign it the value.

Upvotes: 1

Nicolas Goy
Nicolas Goy

Reputation:

I think this should help you:

http://cross-browser.com/talk/inter-frame_comm.html

Upvotes: 1

Steffen
Steffen

Reputation: 2948

Your question is a bit skimpy on the details, but in principle you can find the IFRAME e.g. with document.getElementById() and then use contentDocument on it to get its document. Again with getElementById() you can then access the desired child element.

Obviously you'd have to give both element "id" attributes.

I think IE does not know contentDocument, so you might have to use contentWindow.document there.

Upvotes: 2

Related Questions