0

I was reading this question, but my script is still not working: Can javascript running inside an iframe affect the main page?

I have my main page test.php

<div id="testfield">Some text here</div> 
<iframe id="upload_target" name="upload_target" src="" style="..."></iframe>

In the iframe page I have a button that should be removing what's inside a div on the main page.

<button onclick="goNow();">TEST ME</button>

And this is the Javascript part that is triggered when pressed the button:

function goNow() {
parent.getElementById('testfield').innerHTML = '';
}

This all works on one page with document.getElementById, but once I use the iframe and parent.getElementById it returns an error:

Error: Object doesn't support property or method 'getElementById'
Community
  • 1
  • 1
Nicolas.
  • 453
  • 1
  • 5
  • 27

1 Answers1

3

parent refers to the parent window. You then need to get that window's document to continue.

parent.document.getElementById('testfield').innerHTML = "";
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592