How to pick element inside iframe using document.getElementById
Asked Answered
P

6

95

I have a iframe like this

<iframe name="myframe1" id="myframe1" width="100%" height="100%" src="a.html">
<html>
    <head></head>
    <frameset name="myframe2" cols="0%, 100%" border="0" frameBorder="0" frameSpacing="0">
        <frame name="page1" src="c.html" scrolling="no"></frame>
        <frame name="page2" src="d.html" >
            <html>
                <head></head>
                <body id="top">
                    <div id="div1">
                        <div id="div2">
                            <div id="div3">
                                <ul id="x">
                                    <li>a</li>
                                    <li>b</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </body>
            </html>

        </frame>

    </frameset>
</html>
</iframe>

I want to refer to the element "x". I tried in several ways but I couldn't find a solution.

Pyrophoric answered 22/1, 2013 at 3:58 Comment(1)
Possible duplicate of Get element from within an iFrameQuasimodo
E
216
document.getElementById('myframe1').contentWindow.document.getElementById('x')

Fiddle

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

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

Ethiop answered 22/1, 2013 at 4:8 Comment(2)
The last sentence is very important ;) you frame's source must be on the same domain of your pageOujda
But what if frame's source not belongs to the same domain ? How to solve it? Regards, Mik.Rochdale
B
25

use contentDocument to achieve this

var iframe = document.getElementById('iframeId');
var innerDoc = (iframe.contentDocument) 
               ? iframe.contentDocument 
               : iframe.contentWindow.document;

var ulObj = innerDoc.getElementById("ID_TO_SEARCH");
Broch answered 22/1, 2013 at 4:10 Comment(0)
R
8

(this is to add to the chosen answer)

Make sure the iframe is loaded before you

contentWindow.document

Otherwise, your getElementById will be null.

PS: Can't comment, still low reputation to comment, but this is a follow-up on the chosen answer as I've spent some good debugging time trying to figure out I should force the iframe load before selecting the inner-iframe element.

Reluctivity answered 24/6, 2019 at 15:43 Comment(0)
N
3

You need to make sure the frame is fully loaded the best way to do it is to use onload:

<iframe id="nesgt" src="" onload="custom()"></iframe>

function custom(){
    document.getElementById("nesgt").contentWindow.document;
    }

this function will run automatically when the iframe is fully loaded.

Nitrous answered 30/6, 2020 at 13:29 Comment(0)
P
2

In my case I was trying to grab pdfTron toolbar, but unfortunately its ID changes every-time you refresh the page.

So, I ended up grabbing it with this one-liner:

const pdfToolbar = document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('HeaderItems');

As in the array written by tagName you will always have the fixed index for iFrames in your application.

Pepito answered 26/6, 2019 at 8:5 Comment(0)
N
1

Apologies I don't have enough reputations to add a comment but maybe writing an answer is ok in this case. I've been looking to change the css of some html in an Iframe. I have a bookmarklet code which works outside an iframe so I'm nearly there:

javascript:(function(){ var style = document.createElement('style'), styleContent = document.createTextNode('.notifications-list--cMVsqVbKkaXV4SQ {zoom: 0.8!important;}'); style.appendChild(styleContent); var caput = document.getElementsByTagName('head'); caput[0].appendChild(style); })();

my iframe is called xm-feed-iframe

so I guessed you'd change the code to

document.getElementById('xm-feed-iframe').contentWindow.document.getElementById('x')

But I needed to work with a class in order to change the css styles. So I guess it might be as simple as changing

var caput = document.getElementsByTagName('head')

to

var caput = document.getElementById('xm-feed-iframe').contentWindow.document.getElementsByTagName('head')

so the final code would be

javascript:(function(){ var style = document.createElement('style'), styleContent = document.createTextNode('.notifications-list--cMVsqVbKkaXV4SQ {zoom: 0.8!important;}'); style.appendChild(styleContent); var caput = document.getElementById('xm-feed-iframe').contentWindow.document.getElementsByTagName('head'); caput[0].appendChild(style); })();

And hey presto it worked. I can now delve a little deeper and add some counters hopefully to the list which appears in the iframe! :)

Any comments about the code would be most appreciated as I'm self taught!

Hope this helps someone else

Thanks

UPDATE: Here we are - I know have a numbered list using a bookmarklet. My life will be so much easier now as I will no longer loss track of all the items I need to download. :D

enter image description here

Nason answered 9/2, 2023 at 11:33 Comment(1)
Good answer, from top webpage DOM calling into iframe: document.getElementById('xm-feed-iframe').contentWindow.document.getElementsById('inside-iframe')Hylo

© 2022 - 2024 — McMap. All rights reserved.