6

This is my iframe:

#htmlDiv {
    position: absolute;
    top: 0px;
    left: 300px;
    height: 650px;
    width: 1130px;
}

#htmlFrame {
    position: absolute;
    top: 0px;
    left: 0px;
    margin: 10px 10px 10px 10px;
    padding: 0px 0px 0px 0px;
    height: 630px;
    width: 1110px;
}

<div id="htmlDiv">
    <iframe id="htmlFrame"></iframe>
</div>

$('#htmlFrame').click(function(){
    alert('clicked');
});

But when I click inside the iframe, it doesn't show "clicked".
How do I detect click inside iframe.

Larry Lu
  • 1,609
  • 3
  • 21
  • 28
  • 1
    Possible duplicate of [How to add click event to a iframe with JQuery](http://stackoverflow.com/questions/1609741/how-to-add-click-event-to-a-iframe-with-jquery) – Jaumzera May 24 '16 at 03:22
  • I have tried it but it doesn't work to me. – Larry Lu May 24 '16 at 03:24
  • Take a look at the answer http://stackoverflow.com/questions/2381336/detect-click-into-iframe-using-javascript#answer-23231136 – Masum Billah May 24 '16 at 03:25
  • So try this one: http://jsfiddle.net/4HQc4/, from http://stackoverflow.com/questions/15080222/add-click-event-to-iframe – Jaumzera May 24 '16 at 03:26
  • Possible duplicate of: http://stackoverflow.com/questions/6452502/adding-click-event-handler-to-iframe – Anialation May 24 '16 at 03:30

3 Answers3

6

You cannot detect a click event on an iframe. What can be done is you can place a overlapping layer on top of your iframe and capture the click event of the overlapping div. See below snippet:

$("#overlap").on("click",function(){
  alert("click");
  $("#overlap").css("z-index",-1);

});
$("#htmlFrame").on("mouseleave",function(){
  $("#overlap").css("z-index",1);
});
#htmlDiv {
    position: absolute;
    top: 0px;
    left: 300px;
    height: 65px;
    width: 113px;
}

#htmlFrame {
    position: absolute;
    top: 0px;
    left: 0px;
    margin: 10px 10px 10px 10px;
    padding: 0px 0px 0px 0px;
    height: 63px;
    width: 110px;
}

#overlap{
  position: absolute;
  background-color:trasparent;
  top: 0px;
  left: 0px;
  margin: 10px 10px 10px 10px;
  padding: 0px 0px 0px 0px;
  width: 110px;
  height:63px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="htmlDiv">
    <iframe id="htmlFrame"></iframe>
  <div id="overlap">
</div>
</div>
Senjuti Mahapatra
  • 2,570
  • 4
  • 27
  • 38
0

Your current code is working fine, try to click it to the border of the iframe and the alert will show. I tried to create a solution for you by adding an overlay to the iframe

You can see the DEMO

<div id="htmlDiv">
    <iframe id="htmlFrame"></iframe>
    <div id="overlay">
    </div>
</div>
Larry Lu
  • 1,609
  • 3
  • 21
  • 28
winresh24
  • 687
  • 1
  • 6
  • 27
0

I did this:

<div style="position:relative;">
    <iframe src="..."></iframe>
    <div style="position:absolute;top:0;left:0;right:0;bottom:0;eventual mouse cursor style" onclick="eventual alternative action"></div>
</div>
Luca C.
  • 11,714
  • 1
  • 86
  • 77