views:

856

answers:

1

I have a webpage where there is a texarea within a iframe. I need to read the value of this textarea from its child page javascript. Presently by using window.parent.getelementbyID().value in the javascript, I am able to fetch values of all controls in the parent page except the textarea within the iframe. The frame id n frame name in my parent page changes in runtime, hence we cannot use the frame id/frame name for reference.

Can anyone please give me any pointers to resolve this issue? Thanks in Advance, Archana

+4  A: 
<form name="formname" .... id="form-first">
    <iframe id="one" src="iframe2.html">
    </iframe>
</form>


function iframeRef( frameRef ) {
    return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
}

var inside = iframeRef( document.getElementById('one') )

inside is now a reference to the document, so you can do getElementsByTagName('textarea') and whatever you like, depending on what's inside the iframe src.

meder
Thanks Meder for your inputs.
archana roy
So that worked out fine for you?
meder
Meder..I havent checked it. I will go to office and implement it and let you know. Thanks a lot.
archana roy
Just 1 concern Meder--var inside = iframeRef( document.getElementById('one') )The frameid n framename in my page changes in runtime, so it wont be possible to pass the frameid(as done by you).Is there any other alternative?
archana roy
yes. getElementsByTagName('iframe') to refer to a nodeList and access elements inside with [0], assuming you're dealing with one. or you can do a loop.
meder
Thanks Meder. Just 1 more query--Whats the difference between--'frameRef.contentWindow.document' and 'frameRef.contentDocument'I want to understand the concept behind this.It would be really good of you,if you explain it a bit.Thanks a lot for your help.
archana roy
An explanation can be found @ http://xkr.us/articles/dom/iframe-document/Some browsers support one of those dom properties, and I just check for either.
meder
You r a genius Meder.Thanks a ton for your instant help.
archana roy