0

I am desperately trying to get an element by its ID, which is located in another frame (NOT iframe). See following code to make things clear:

var elem = parent.frames['top'].document.getElementById('mydiv');
alert(elem); // returns null

.

<div id="mydiv">
...
</div>

If I alert parent.frames['top'].document I do get [object] returned. Am I missing something here?

Naveen
  • 6,786
  • 10
  • 37
  • 85
seph
  • 674
  • 2
  • 8
  • 23
  • possible duplicate http://stackoverflow.com/questions/1088544/javascript-get-element-from-within-an-iframe – karaxuna Mar 22 '13 at 11:30
  • 1
    alert won't show you the contents o f the object. use `console.log()` instead. – Adam Tomat Mar 22 '13 at 11:33
  • 1
    That is correct, because a DOM element is an object in JS and the default string representation of an object is `[object <[[Class]]>]`. It seems there is nothing wrong with what you are doing. – Felix Kling Mar 22 '13 at 11:33
  • thanks for the answers. i just wanted to clarify that i get `null` returned when using getElementById. So I guess there is something wrong here, or did I missunderstood something? – seph Mar 22 '13 at 12:10

2 Answers2

0

If it is possible to use jQuery. You can achieve this as below

$("some selector", top.frames["frame_name"].document))
Varun
  • 1,014
  • 8
  • 23
0

The code parent.frames['top'] returns a Window object. So you would have to modify your code as

var elem = parent.frames['top'].window.document.getElementById('mydiv');
Naveen
  • 6,786
  • 10
  • 37
  • 85