Sergey Sergey - 2 months ago 6
HTML Question

document.getElementById doesn't retrieve an element that actually exists

Here is the DOM that clearly contains an input with id=

RESULT_TextField-11
:
enter image description here

But when I try to retrieve it from Chrome console using document.getElementById method, it doesn't work:

enter image description here

document.getElementsByName doesn't work either.

Why could this happen? The first screenshot was taken before the second one.

What's interesting is that when I try to run the same code in console a few minutes later, it often works.

Some information that may help:


Answer

The input can't be found because it's within an iFrame. You would have to target it with something like this instead:

document.getElementById('frame-one997541297').contentWindow.document.getElementById('RESULT_TextField-11')

However, that won't work either because the iFrame is loaded from another domain. So scripts at your site won't be able to interact with content from the iFrame's site due to the same origin policy.