25

I need to access and element from within a frameset frame. For example if I have the following markup:

<frameset rows="33%,33%,*">
  <frame src="frame1.html"/>
  <frame src="frame2.html"/>
  <frame src="frame3.html"/>
</frameset>

How can I get some element from one of the child frames? I have tried this:

window.frames[1].getElementById('someElementId')

This results in a type error :

getElementById() is not a function.

Can someone assist?

Thanks!

Munawir
  • 3,346
  • 9
  • 33
  • 51
Nick
  • 19,198
  • 51
  • 185
  • 312

3 Answers3

30

You need to get the Document object for the frame.

window.frames[1].document.getElementById('someElementId')
morgancodes
  • 25,055
  • 38
  • 135
  • 187
9
<frameset rows="33%,33%,*">
<frame id="demo" src="frame1.html"/>
<frame src="frame2.html"/>
<frame src="frame3.html"/>
</frameset>

Answer:

document.getElementById("demo").contentDocument.documentElement.innerHTML;
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Vinod
  • 503
  • 4
  • 8
7

You can try using framename as well

window.frames['frame_name'].document.getElementsByName('element_name');   
Praveen_07
  • 170
  • 2
  • 12
  • this does not return windows object hence will not work while window.frames[1] return windows object hence works. – KlwntSingh Jun 29 '15 at 05:08
  • getting frame by name here which is a string i.e frame_name , gave me an error , `element implicitly has an 'any' type because index expression is not of type 'number'` , had to set `suppressImplicitAnyIndexErrors` to true in tsconfig.json – devcodes Aug 07 '19 at 09:23