-1

I have an iframe with external content on my website. In the code of the iframe content is a button with the id "XYZ"

I would like to set an onclick function on this. I tried something like that:

$( "#XYZ" ).click(function() {
alert("CLICK");
});

but this doesn't work > no alert. Where is my mistake?

Trombone0904
  • 4,132
  • 8
  • 51
  • 104

1 Answers1

0

The Iframe has its own context. You can't access it directly like that using Jquery. You would need to get the iFrame contents first.

$('#frame').contents().find('#XYZ').on('click', () => alert('CLICK');

You might have some permission error because some Iframe have a same origin policy

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • yes, I have a permission error. What can I do ? The Alert will not come up – Trombone0904 Jan 07 '20 at 13:55
  • 1
    Well there is not much you can do. You don't own the code in the iframe. We don't have much information about the context of what you are trying to do so we can't help much but i'd say: use something else than an iframe. – Nicolas Jan 07 '20 at 13:57