I have a page that contains many children such as iframe, frameset, frame etc.
I would like a function like document.getElementById(targetId) that returns an element, but also searches in the documents of all frames.
I would prefer a solution that doesn't require any extentions like jQuery.
asked Aug 14, 2011 at 22:30
ahoo
1,3812 gold badges17 silver badges38 bronze badges
1 Answer 1
You can do
my_iframe_reference.contentDocument.getElementById(targetId)
to search by id in a single frame.
If you want to search on all the frames you can iterate over the window.frames array.
for(var i=0; i<window.frames.length; i++){
var node = window.frames[i].contentDocument.getElementById('id');
if(node) return node;
}
answered Aug 14, 2011 at 22:35
hugomg
70.4k30 gold badges168 silver badges257 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
ahoo
thanks, i tested it but get this error
contentDocument is undefined. my browser is ie8hugomg
Try
contentWindow.document instead: stackoverflow.com/questions/1477547/… ahoo
frames[] only returns an array of
iframes no frames, at least in IElang-js