views:

1878

answers:

4

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
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.
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
Maybe you need to add .document before .getElementById()
Kristinn Örn Sigurðsson
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
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.
A: 

Try giving a name to your iframe and acess the iframe like this

window.parent.<iframe_name>.document.getElementById()
Umesh
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.
A: 

Assuming that your page and frame structure is as follows

parent (contains iframe)
   |--> child page (attempts to access iframe in parent)

and that

  1. the src of the iframe and other pages is the same
  2. 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
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.
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
Thanks Vineet. I will try to implement this and let you know if it works.