JAVA Script/Answer Detail

How to pick element inside iframe using document.getElementById

30 Points
10 Posts

I have with iframe as:

<iframe id="myIframe"></iframe>

In the iframe I have hidden field as

<input id="hdnSelected" name="hdnSelected" type="hidden" value="myhiddenvalue" />

I want the value of this hidden input field on parent page.

Total Answered: 2
Total Marked As Answer: 0
Posted On: 08-Jan-2018 03:16
Share:   fb twitter linkedin
10 Points
5 Posts
Posted On: 08-Jan-2018 05:27

Use following javascript code:

var hdnSelectedInput = document.getElementById('myIframe')

contentWindow is supported by all browsers including the older versions of IE.

Note: If the iframe's src is from another domain, you won't be able to access its content due to the Same Origin Policy. 

The last sentence is very important ;) your frame's source must be on the same domain of your page
 - Rahul Kiwitech  08-Jan-2018 05:33
28 Points
14 Posts
Posted On: 09-Jan-2018 23:07
var x = document.getElementById("myIframe");
var y = (x.contentWindow || x.contentDocument);
if (y.document)
    y = y.document;
var hdnSelectedInput = y.getElementById('hdnSelected');

The contentDocument property returns the Document object generated by a frame or iframe element.


Active User (1)

 Log In to Chat