1

I have embeded jQuery mobile page using in my HTML page, but when I try to access its internal div tag using jQuery, the find method returns nothing.

HTML With jQuery

<iframe id="myframe" src="../codiqa/mobile.htm" 
    style="width: 500px; height: 520px; 
    border: 1px solid #ccc; margin-top:20px;">
</iframe> 

here

$("#myframe").load(function(){
    alert($(this).contents().find('#mobilebutton').html());     
});

mobile code in iframe

<div id="mobilebutton" data-role="button">Mobile Button</div>
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
user1175106
  • 89
  • 2
  • 9

4 Answers4

1

Use window.load instead of $("#myframe").load. The elements of frame will be ready on $(window).load event and you will be able to access them.

$(window).load(function(){
    alert($("#myframe").contents().find('#mobilebutton').html());     
});
Adil
  • 146,340
  • 25
  • 209
  • 204
0

According to jquery API guide: .html() "Get the HTML contents of the first element in the set of matched elements"

this way you need to add .parent in front of it to get the directed element. in this case:

$("#myframe").load(function(){
alert($(this).contents().find('#mobilebutton').parent.html()); });

In case you want to get the html of the button

Toping
  • 754
  • 5
  • 16
0

If @Adil solution doesn't work, you can try to add dynamically the iframe to attach the load event using this function:

function callIframe(url, callback) {
    $(document.body).append('<iframe id="myframe" 
                             style="width: 500px; height: 520px; 
                             border: 1px solid #ccc; margin-top:20px;">
                            </iframe> ');
    $('myframe').attr('src', url);
    $('myframe').load(function() 
    {
        alert($(this).contents().find('#mobilebutton').html())
    });
}

How to use it?

$('document').load(callIframe('../codiqa/mobile.htm'));

Further:

Community
  • 1
  • 1
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
0

It Works fine on IE.

Try it

alert($(this).contents().find('#mobilebutton').html());
Govinda Rajbhar
  • 2,926
  • 6
  • 37
  • 62
user1175106
  • 89
  • 2
  • 9