I am trying to access a specific element (perhaps more similar to this) using an iframe and jQuery object , but it does not work.
The iframeWindow
object iframeWindow
not null , but the following statement does not work. I saw something like this in this answer, but this does not work. Am I doing something wrong?
Here is my code:
RADIO.PHP
<!DOCTYPE html> <html> <head> <title></title> <script type="text/javascript" src="jquery.js"></script> <script> $(document).ready(function(){ setTimeout(function(){ var iframe= document.getElementById("iframe"); var iframeWindow = iframe.contentWindow; var text=iframeWindow.$("div:nth-child(3) .c2").html(); console.log(text); </script> </head> <body> <div class="c1"> <iframe id="iframe" src="api.php" height="200" width="300"> </iframe> </div> </body> </html>
API.PHP
<!DOCTYPE html> <html> <head> <title></title> </head> <script type="text/javascript" src="jquery.js"></script> <body id="abody"> Hey <div class="c1"></div> <div class="c1"> <p class="c2"></p> </div> <div class="c1"> <p class="c2"> INNER MOST </p> </div> </body> </html>
EDIT: I fixed the syntax errors.
source share