1

How can I get click event from iframe div in my web page where i hade placed iframe using jquery?

Valisimo
  • 11
  • 1
  • 2
  • 3
  • Are you wanted to hook a click event in a `
    ` element hosted _in_ the iframe from the parent page that has the iframe in it?
    – Chad May 05 '11 at 19:09
  • possible duplicate: [How to add click event to a iframe with JQuery - Stack Overflow](http://stackoverflow.com/questions/1609741/how-to-add-click-event-to-a-iframe-with-jquery) – MikeM May 05 '11 at 19:11

2 Answers2

4

Use jQuery contents to interact with iframe contents.

var frame = $('<iframe />').appendTo('body');  // create the iframe and append it to the body
var frameBody = frame.contents().find('body'); // grab the body node
frameBody.append('<div />');                   // add a div
frameBody.find('div');                         // retrieve the div

If you are loading the iframe from a url make sure you adhere to the same-origin policy, and wait for the iframe to load:

frame.load(function(){
    var frameBody = frame.contents().find('body');
    frameBody.find('div').click(function(){
        // here is the iframe div click callback
    }); 
});
Josiah Ruddell
  • 29,697
  • 8
  • 65
  • 67
0

This may help you @Valisimo. This is work in internal domain only.

$(document).ready(function(e) {
    // here , I write <h2> HTML tags to try.

    $('#frame').contents().find('h2').click(function(){
        $('#frame').contents().find('h2').toggle(1000);
    })
});
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
C.Gembel
  • 1
  • 1