JS of a document working within iframe html

Asked

Viewed 110 times

2

Good afternoon, you guys!

I have my site for example, which has the normal page that every page has, html, head, body, etc... Inside this my page, has an iframe, this iframe pulls another html from another page, getting more or less like this:

<html>
<head></head>
<body>
     <iframe>
         <html>
            <head></head>
            <body>
                <div class="teste">Teste</div>
            </body>
         </html>
     </iframe>
     <script>
          $('.teste'); /*Está me retornando null*/
     </script>
</body>
</html>

The problem is this, I can’t touch the html of this iframe, I don’t have access to it. If I try to get the value of that test div there with a document.querySelector('.teste') with jQuery himself with the $('teste'); does not work, because that Document there refers to my document (what I have access to) the iframe document I can not.

You’re always returning me null.

How can I fix this?

1 answer

2


You need to select the iframe and then use contents:

$("iframe").contents().find(".teste");

If iframe has a better ID:

$("#idIframe").contents().find(".teste");

Reference: api.jquery.com/Contents

Browser other questions tagged

You are not signed in. Login or sign up in order to post.