Finglish Finglish - 16 days ago 5x
Javascript Question

How to get containing iframe id when the iframe document is clicked in jquery

I am looking for a way to use jQuery to get the id of an iframe when its document element is clicked. The code will be run initialed from the parent window.

$frame = $("<iframe class='frame'></iframe>").load(function(){
$(this).contents().bind("click", function(e){

the alert reads undefined.

Alternatively I also tried:

alert($(this), window.parent).attr("id");


You'll notice that the previous example only works when you give your iframe a border and click on that (i.e. clicking on the iframe element, not on the document element it contains, which I believe was what you asked for).

The following code binds a mouseover handler to the iframe, which grabs the iframe's ID before binding an onclick handler to its contents. Note that this will only work if the iframe points at another page on your site, not one on another domain.


       $('iframe').bind('mouseover', function(){

          var iframeID = $(this).attr('id');


          $(this).contents().bind('click', function(){