How to i select div id onClick event in iframe?

Discussion in 'jQuery' started by D3rek, Apr 4, 2012.

  1. #1
    This is my code. I am trying to load a website in a iframe. When the website is loaded, i would like to click in the iframe to select the div id (where i clicked)

    <iframe name="frameID" id="frameID" src="http://www.test.com" style="width:100%;height:400px;"></iframe>
    
    $(document).ready(function(){
    $('#frameID').load(function(){
    
    
        $('#frameID').contents().bind("click", function () {
            var a = $(this).id;
            alert (a);
            return false;
        });
    });
    Code (markup):
    For example, if i clicked in the footer of the website loaded in the iframe, i would like to display the div id "footer".

    However, i am unable to do so, it keep returning me undefined.
     
    D3rek, Apr 4, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    Install firebug extension and then add the following sentence:

    var a = $(this).id;
    console.log($(this));

    This console.log() statement should log the contents of this and it should help you one step forward.
     
    e-abi, Apr 14, 2012 IP
  3. D3rek

    D3rek Well-Known Member

    Messages:
    1,345
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    100
    #3
    solved it. I use jquery on method. :)
     
    D3rek, Apr 15, 2012 IP