I have 2 pages-- Parent n child. In the parent page i have an iframe whose value i want to ftech in the javascript of child page. Is there any way?....Please suggest.
A:
Hi.
You can use
window.parent.getElementById('YOUR ID').value();
to get the value from the input element with the id "YOUR ID" from the parent.
Kristinn Örn Sigurðsson
2009-09-20 04:28:48
Thanks Kristinn for your suggestions.But I have tried using the same. It returns - "undefined".By using your solution, I am able to fetch values of all the elements in the parent page, but not the ones inside an iframe.Is there some other way to fetch values incase of an iframe?Request to share your knowledge.
2009-09-20 04:41:16
I haven't tested this but I imagine it would be done in this way.window.parent.frames['iframe_name'].getElementById('element id').value;the 'iframe_name' is of course the name you have on the <iframe> caller.
Kristinn Örn Sigurðsson
2009-09-20 05:16:34
Maybe you need to add .document before .getElementById()
Kristinn Örn Sigurðsson
2009-09-20 05:17:39
A:
Use
parent.getElementById('elementId').value;
to get the element. If you have multiple nested iframes, you can get the root parent by using
top.getElementById('elementId').value;
Either will work in your case.
Cory Larson
2009-09-20 04:37:58
Thanks Cory for your suggestions.But I have tried using the same. It returns - "undefined".By using your solution, I am able to fetch values of all the elements in the parent page, but not the ones inside an iframe.Is there some other way to fetch values incase of an iframe?Request to share your knowledge.
2009-09-20 04:45:17
A:
Try giving a name to your iframe and acess the iframe like this
window.parent.<iframe_name>.document.getElementById()
Umesh
2009-09-20 04:44:52
Thanks a lot for the suggestions Umesh.My page n frame structure is--parent(contains iframe)|--> child page javascript attempts to access iframe in parent.In my case the iFrame name n id in parent gets generated dynamically everytime a page is loaded. So how to handle such situation?..Request you to share your knowledge.
2009-09-20 04:52:54
A:
Assuming that your page and frame structure is as follows
parent (contains iframe) |--> child page (attempts to access iframe in parent)
and that
- the src of the iframe and other pages is the same
- the name of the iframe is 'iFrame',
you can access an element named 'iFrameElement' in the iframe using the following JavaScript statement from the child:
parent.frames['iFrame'].document.getElementById('iFrameElement').value;
or simply the following from the parent containing the iframe
frames['iFrame'].document.getElementById('iFrameElement').value;
Since the frame name is indeterminate at runtime, you could revert to using the frame number in the window.frames array, as follows (from the child)
//assuming frames[0] refers to the iframe
parent.window.frames[0].document.getElementById('iFrameElement').value;
or from the parent
//assuming frames[0] refers to the iframe
window.frames[0].document.getElementById('iFrameElement').value;
Vineet Reynolds
2009-09-20 04:45:06
Thanks a lot for the suggestions Vineet.My page n frame structure is--parent(contains iframe)|--> child page javascript attempts to access iframe in parent.In my case the iFrame name n id in parent gets generated dynamically everytime a page is loaded. So how to handle such situation?..Request you to share your knowledge.
2009-09-20 04:51:30
You can pass the frame name and id to the child, or you can avoid referencing the frame by name, and instead refer to it by frame number.
Vineet Reynolds
2009-09-20 04:58:36