Oliver Watkins
Oliver Watkins

Reputation: 13519

Trying to select elements across iframes

I want to select all DIVs in my page, including its child iframe.

I have two DIVs here but whenever i try and select them it only grabs the outer one.

<html>
    <head></head>
    <body>
    <div class='xx'>blah</div>

    <iframe id='x'>
        <html>
            <head></head>
            <body>
                <div class='xx'>blah2</div>
            </body>
        </html>        
    </iframe>
    </body>
</html>   

Is there any way for me to get both DIVs back?

var a = $('.xx');

alert(a.length); //only gives me 1 :(

My fiddle is here :

http://jsfiddle.net/7kvFw/

Upvotes: 0

Views: 81

Answers (1)

rekire
rekire

Reputation: 47955

With only one call this is not possible at all. The iframe is another document so it is not accessable directly. You need to search in all frames seperatly.

By the way your example is not valid. A iframe is just a referece to another document you cannot put the content in the same html document. If you just care about a "box" with the option to scroll inside, just add the another div with the ablity to scroll. This would also allow you to get all .xx elements at once.

See also this fiddle.

Upvotes: 2

Related Questions