By archana roy

2009-09-21 04:12:03 8 Comments

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.


@meder omuraliev 2009-09-21 04:20:19

If you have the HTML

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

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.

@archana roy 2009-09-21 05:23:28

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?

@meder omuraliev 2009-09-21 05:24:38

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.

@archana roy 2009-09-21 06:09:06

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.

@meder omuraliev 2009-09-21 06:11:58

An explanation can be found @ Some browsers support one of those dom properties, and I just check for either.

@archana roy 2009-09-21 06:24:56

You r a genius Meder. Thanks a ton for your instant help.

@Doug Amos 2013-07-03 10:37:36

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

var inside = $('#one').contents();

@codeymcgoo 2015-11-19 07:49: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."

Related Questions

Sponsored Content

98 Answered Questions

[SOLVED] How can I remove a specific item from an array?

  • 2011-04-23 22:17:18
  • Walker
  • 7158407 View
  • 8677 Score
  • 98 Answer
  • Tags:   javascript arrays

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

59 Answered Questions

[SOLVED] How do I check if an element is hidden in jQuery?

95 Answered Questions

[SOLVED] How to validate an email address in JavaScript

46 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

61 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

28 Answered Questions

3 Answered Questions

32 Answered Questions

[SOLVED] How can I change an element's class with JavaScript?

  • 2008-10-12 20:06:43
  • Nathan Smith
  • 2670281 View
  • 2843 Score
  • 32 Answer
  • Tags:   javascript html dom

Sponsored Content