Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a click event to p elements in iframe (using jQuery)

How to add a click event to <p> elements in iframe (using jQuery)

<iframe frameborder="0" id="oframe" src="iframe.html" width="100%" name="oframe">
like image 400
faressoft Avatar asked Sep 14 '10 12:09

faressoft


2 Answers

There's a special jQuery function that does that: .contents(). See the example for how it's works.

like image 121
jerone Avatar answered Oct 24 '22 15:10

jerone


Wanted to add this, as a complete, copy-paste solution (works on Firefox and Chrome). Sometimes it is easy to miss to remember to call the event after the document, and so the iframe, is fully loaded:

$('#iframe').on('load', function() {
    $('#iframe').contents().find('#div-in-iframe').click(function() {
        // ...
    });
});

The iframe must be on the same domain for this to work.

like image 20
Envayo Avatar answered Oct 24 '22 15:10

Envayo