37

I have a webpage where there is a textarea within an iframe. I need to read the value of this textarea from its child page using 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.

Can anyone please give me any pointers to resolve this issue?

danwellman
9,4018 gold badges65 silver badges92 bronze badges
asked Sep 20, 2009 at 14:57
5
  • Can anyone help me please with the logic? Commented Sep 20, 2009 at 15:52
  • possible duplicate of How can I access iFrame elements with Javascript? Commented Dec 16, 2012 at 21:00
  • 1
    Use window.frames['myIFrame'].contentDocument.getElementById('myIFrameElemId') which works in modern browsers (e.g. Firefox 35) -- and pay attention to the same-origin policy Commented Feb 18, 2015 at 2:49
  • @CAW is absolutely right its working now Commented May 14, 2020 at 12:17
  • Consider to use cross-document messaging. stackoverflow.com/questions/9153445/… Commented Jun 1, 2022 at 20:35

6 Answers 6

69

If your iframe is in the same domain as your parent page you can access the elements using window.frames collection.

// replace myIFrame with your iFrame id
// replace myIFrameElemId with your iFrame's element id
// you can work on window.frames['myIFrame'].document like you are working on
// normal document object in JS
window.frames['myIFrame'].document.getElementById('myIFrameElemId')

If your iframe is not in the same domain the browser should prevent such access for security reasons.

greybeard
2,6779 gold badges40 silver badges71 bronze badges
answered Sep 20, 2009 at 17:00
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot for your suggestions. But the problem is that the frame id n frame name in my parent page changes in runtime, hence we cannot use the frame id/frame name for reference. Is there any other way Ra Yell?..
Bith parent n child page are in same domain.
If there is ony one iframe on your site you could try using document,frames[0].document. If there are more you need to change the index to some other value.
document.frames no longer works. Use window.frames instead.
.contentDocument rather than document works for me
|
18

window.frames['myIFrame'].document.getElementById('myIFrameElemId')

not working for me but I found another solution. Use:

window.frames['myIFrame'].contentDocument.getElementById('myIFrameElemId')

I checked it on Firefox and Chrome.

answered Dec 23, 2013 at 19:49

2 Comments

I keep hitting a security error even though the web pages are both offline.
dont look anything else this is the actual answer, superb
15

You should access frames from window and not document

window.frames['myIFrame'].document.getElementById('myIFrameElemId')
nhahtdh
56.9k15 gold badges131 silver badges164 bronze badges
answered Nov 1, 2011 at 17:44

Comments

3

Make sure your iframe is already loaded. Old but reliable way without jQuery:

<iframe src="samedomain.com/page.htm" id="iframe" onload="access()"></iframe>
<script>
function access() {
 var iframe = document.getElementById("iframe");
 var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
 console.log(innerDoc.body);
}
</script>
answered Mar 14, 2017 at 9:45

Comments

3

Two ways

window.frames['myIFrame'].contentDocument.getElementById('myIFrameElemId')

OR

window.frames['myIFrame'].contentWindow.document.getElementById('myIFrameElemId')
answered Apr 11, 2018 at 9:39

Comments

2

this code worked for me:

window.frames['myIFrame'].contentDocument.getElementById('myIFrameElemId');
ReactiveRaven
7,6392 gold badges31 silver badges39 bronze badges
answered Oct 28, 2016 at 14:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.