2

I have an IFRAME and I want to trigger an event (which is inside the iframe) from the parent of an IFRAME:

Just a rough example of what I am trying to do :

<iframe id="i">
  <div id="test"></div>
  <script>
  $(document).ready(function() {  // Event is binded inside the iframe 

   $("#test").live({ click : function() { alert("hello"); } });
  });
  </script>
</iframe>

<script>
$(document).ready(function() { // Want to trigger it from outside the iframe
 $("#i").contents().find("#test").trigger("click");
});
</script>
2
  • 1
    you can't write your code inside iframe tag Commented Jun 8, 2012 at 6:00
  • @Somebodyisintrouble Please notice JUST A ROUGH EXAMPLE in my question Commented Jun 8, 2012 at 6:02

2 Answers 2

8

Your jquery is correct only problem is that that you are doing this when document is ready but at that time iframe is not getting fully loaded therefore div doesn't exist at that time and not triggers click. Change your main page script to

$(document).ready(function() {
    //want to trigger it from outside the iframe
    $("#i").load(function(){
        $("#i").contents().find("div").trigger("click");
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

try this

ger javascript event from within an iFrame

but look at answer from Gilly

document.myFrameName.myFunction(); 

regards

1 Comment

but it will not trigger click

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.