0

I have used JQuery to load a website, like this:

$("#external-site")
   .html('<object class="website" data="http://www.example.com'"><object/>');

into the #external-site div. This puts an <object> into the #external-site div and inside that is a html document which looks like this: #document. I want to attach an on click event to an element in the document. I have tried things like this:

$( ".myDiv" ).on( "click", function() {
    console.log("hello")
});

Then, I tried a delegated event:

$( "#document" ).on( "click", ".myDiv", function() {
    console.log("hello")
});

But neither would log to the console when I click on the .myDiv element. Am I referencing the #document correctly? Is it possible what I am trying to do?

Haych
  • 932
  • 13
  • 36
  • usually document will be selected as $(document).. Is there any element in your html holds document as id? – Vishnu May 22 '17 at 18:18
  • I tried selecting it like that but it didn't work. There is no element with document as id. – Haych May 22 '17 at 18:19
  • Depending on the security settings of the third party website you may be being blocked by the SOP and various other cross-domain security measures. Check the console for errors. Unless the website you're loading expressly allows themselves to be loaded in an iframe and have the content amended by an external source (which is *highly* unlikely) then this will most likely not work – Rory McCrossan May 22 '17 at 18:19
  • Ah ok. If the iframe content is on the same domain it should work. Try this: https://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery – Rory McCrossan May 22 '17 at 18:21
  • Sorry, it is not on the same domain but i created it and it is on a different domain. – Haych May 22 '17 at 18:21
  • Ok, in that case you need to set your content to the correct security settings. The issue will 100% be down to the Same Origin Policy (SOP) – Rory McCrossan May 22 '17 at 18:22

1 Answers1

0
 I have made this in jsfiddle. Is this what you are looking for?

https://jsfiddle.net/s1Lyr9at/1/

Vishnu
  • 127
  • 6
  • 1
    Post the code here, not just at jsfiddle. You can use [Stack Snippets](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) to make it executable. And the text of your answer should not be put into a code block. – Barmar May 22 '17 at 18:42
  • I joined stackoverflow just 3 days before. I dont aware of stack snippets and comment aligning – Vishnu May 22 '17 at 18:45