javascript document.getElementById in other frames
Asked Answered
S

7

21

I have two frames and want to access an element in one frame from another:

Frame 1:

<div id='someId'>...</div>

Frame 2:

var div=document.getElementById('someId');

div.innerHTML='something'; 

This is somehow not functioning in Firefox so I want to be sure, can I access an element in another frame by its ID?

Straightaway answered 27/11, 2009 at 9:40 Comment(0)
T
27

You can refer the other frame by using

window.frames["framename"]

and then you can refer the element in the DOM by using

window.frames["framename"].document.getElementById ( "yourelementid" );
Thundercloud answered 27/11, 2009 at 9:45 Comment(4)
so, there is no option that browser would find first available element with desired ID no matter in which frame that reside ?Straightaway
@as: getElementByid is restricted to looking in the document object of which it is a method. Bear in mind that a frame is in fact a separate window object, and an examination of the window->document hierarchy should make it clear why a method on one document cannot examine a document in a different window.Nonproductive
It's not wirking in chrome, window.frames['framename'].document has nothing inside it. I mean windows.franes['framename'].ducumnet.getElementById(...)... results Uncaught TypeError: Cannot call method 'getElementByName' of undefined error.Estus
This doesn't work for me, The answer from @Brian Duncan do!Sexlimited
V
13

The issue may be the current frame you are in. If window.frames['framename'] does not work, try parent.frames['framename'] to access the top level frames.

if(parent.frames && parent.frames['framename']) {
   var elem = parent.frames['framename'].document.getElementById(...); 
   // etc
}
Vidavidal answered 18/12, 2014 at 20:22 Comment(0)
W
1

I was having problem with the JS version but was able to use these examples for a working jQuery version:

var obj = $('#yourelementid', parent.frames["framename"].document);
Willette answered 7/1, 2016 at 18:50 Comment(0)
W
1
document.getElementById('frameId').contentDocument.getElementById('frameChild');
Weinstock answered 15/5, 2020 at 21:13 Comment(0)
D
0

Or, if you feel like trying your luck, you can just use a numeric parameter.

window.frames[0].document

Donettedoney answered 13/10, 2017 at 17:39 Comment(0)
F
0

For some reason

window.frames["framename"].document.getElementById ( "yourelementid" );

was not working for me.

This other method did:

   document.getElementById('framename').contentWindow.document.getElementById('yourelementid');
Fimble answered 26/5, 2019 at 18:40 Comment(0)
S
0

Simply use frameId.MyElementId

Of course, when defining your frame, not enough to use "name=....", rather use "id=..." within your frame tag. E.g.:

    <iframe id=MyFrame> .... 
          <input id=MyElementId>
               ...

    <script>
       // do something with MyElementId
           MyFrame.MyElementId.value = "something";

At least, it works as of 2022 (almost 2023).

[Note that I didn't even use "name"... that's quite not necessary by these days, unless you want to use radio buttons with multiple <INPUT's that refer to the same mutually exclusive element]

Supercilious answered 30/12, 2022 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.