Javascript – Can’t set parent hidden input from iframe!

htmljavascriptPHP

I have an iframe on my main page, and this iframe uses php code mostly (src is php file).

At the bottom of this iframe I have this:

   window.parent.document.getElementById("qry_str").value='hey';

And in the main (parent) file, I have this hidden input which I am trying to set from the iframe with js:

         <input type="hidden" id="qry_str" name="qry_str">

Nothing happens when I try to set the value of this hidden input from inside the iframe.
(Nothing shows up in the src code that is)!
I have also tried setting the hidden inputs 'OnChange' event to an alert, but it won't alert because it isn't changed I guess!

I know it finds the element because I have tried to alert the elementId and it works, the element is found!

Thankful for any help!

Best Answer

If the iframe is on the same domain you want to use window.top to access the parent window.

   window.top.document.getElementById("qry_str").value='hey';