How to access child element with javascript?
Im familiar with accessing parent windows like this:
window.parent开发者_JS百科.document.getElementById("ad_nr")
But what if I want to access a child window from a parent? (iframe for example) and then set a hidden input in there to some value created in the parent window, how is this done?
Probably easy but I have missed it, so Im asking you guys!
Thanks
You can use the window.frames
collection.
For example:
window.frames[index].document.getElementById('myInputName').value = someValue;
frames["FRAME_NAME_HERE"].document.getElementById("HiddenField1").value = "Whatever";
Note: the document in the iframe must reside on the same domain as the parent. There is no getting around this restriction.
You can use the window.frames array to access child frames. Example:
window.frames[0].document.getElementById('myElement')
Just access the frames collection and use the document in the usual way. For example:
window.frames['loader_frame'].document.getElementById("ad_nr")
精彩评论