2

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

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, i tested it but get this error contentDocument is undefined. my browser is ie8
Try contentWindow.document instead: stackoverflow.com/questions/1477547/…
frames[] only returns an array of iframes no frames, at least in IE

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.