Accessing DOM element of child IFRAME in same domain

metalaureate
metalaureate used Ask the Experts™
on
I'm trying to access a child element inside an IFRAME, all same domain. How do I do that? The element has an ID. I'm blanking out on this one.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
Use : document.getElementById("myframeID").contentWindow.document

http://www.w3schools.com/jsref/prop_frame_contentwindow.asp
for just javascript this should get you going..

var iframe = document.getElementsByTagName('iframe')[0],
    iDoc = iframe.contentWindow     // sometimes glamorous naming of variable
        || iframe.contentDocument;  // makes your code working :)
if (iDoc.document) {
    iDoc = iDoc.document;
    iDoc.body.addEventListener('afterLayout', function(){
                        alert('works');
                });
};



but if you have jQuery loaded..

            $('#frame_SIA').ready(function() {
                  var x=document.getElementById("frame_SIA");
                  var y=(x.contentWindow || x.contentDocument);
                  $(y.document).bind('keydown', 'ctrl+s', function(evt) {
                        alert('got there');
                  });
            });      

which is way better... Cheers -Jeremy

Author

Commented:
Thanks guys. I can't close this question - something is jacked with EE, doesn't let me Accept answers.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial