0

I have a div id = "tabs2" which contains 4 iframe, I want when I click on an iframe, I recovered its id, I did that but it did not work:

    $("document").ready(function() {
       elm= document.getElementById("tabs2");
       frames=elm.getElementsByTagName("iframe");
       for(var i=0; i<frames.length; i++){
            frames[i].click(function(){
               alert(this.id);
            })
       }
    });
bipen
  • 36,319
  • 9
  • 49
  • 62
FRIDI Mourad
  • 87
  • 1
  • 6
  • 16

2 Answers2

1

Try this.

$('iframe').on('click', function(){
    alert($(this).attr('id'));
})
Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33
  • 1
    $('iframe').click( function(){ alert($(this).attr('id')); }); Works – Pratik Joshi Mar 07 '14 at 10:21
  • when I click on the border, I recovered the id. My iframe contains a youtube video. how do I recover retrieve the id when I click the middle of the iframe (eg the play button) – FRIDI Mourad Mar 07 '14 at 10:35
0

using jquery.

if you need an id whenever the iframe inside the element tabs2 is clicked , then

try this

 $('#tabs2').find('iframe').click(function(){
      alert(this.id);
 });

else

$('iframe').click(function(){
   alert(this.id);
 })

this tracks the click event of all the iframe that is currently present in the document

bipen
  • 36,319
  • 9
  • 49
  • 62
  • when I click on the border, I recovered the id. My iframe contains a youtube video. how do I recover retrieve the id when I click the middle of the iframe (eg the play button) – FRIDI Mourad Mar 07 '14 at 10:42