73

I have a webpage where there is a textarea 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 and frame name in my parent page changes in runtime, hence we cannot use the frame id/frame name for reference.

Amir
  • 1,328
  • 2
  • 13
  • 27
archana roy
  • 1,147
  • 3
  • 11
  • 11
  • 2
    Exact duplicate: http://stackoverflow.com/questions/1451208 – Grant Wagner Sep 21 '09 at 17:10
  • @archana: RaYell's answer http://stackoverflow.com/questions/1451208/1451455#1451455 includes a comment from the first time you asked this question. If you do not know the frame id or name, you can use `document.frames[0].document.getElementById()` (or some other index if it is not the first ` – Grant Wagner Sep 21 '09 at 17:13
  • 2
    Possible duplicate of [Access iframe elements in JavaScript](https://stackoverflow.com/questions/1451208/access-iframe-elements-in-javascript) – Daniel Beck Jun 12 '17 at 12:59

2 Answers2

88

If you have the HTML

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

and JavaScript

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.

Owen Blacker
  • 4,117
  • 2
  • 33
  • 70
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 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 Sep 21 '09 at 05:23
  • 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 omuraliev Sep 21 '09 at 05:24
  • 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 Sep 21 '09 at 06:09
  • 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 omuraliev Sep 21 '09 at 06:11
  • You r a genius Meder. Thanks a ton for your instant help. – archana roy Sep 21 '09 at 06:24
19

Using jQuery you can use contents(). For example:

var inside = $('#one').contents();
Doug Amos
  • 4,243
  • 1
  • 22
  • 23
  • 21
    An important consideration here is that the contents method can "get the content document of an iframe, if the iframe is on the same domain as the main page." – codeymcgoo Nov 19 '15 at 07:49