0

I am working with a system that I have little control over. I would like to add some third-party javascript/jQuery (there is a mechanism to do this) to alter some hyperlinks within an iFrame.

The problem is, that the iFrame itself is brought in via AJAX. When I try to interact with it, jQuery cannot find it in the document. Here is some code I have attempted to use with no success:

$(document).on('click', '#cust_iframe_2', function(){
    alert('found');
});

The element is definitely there when I inspect the source. What am I missing?

Daniele
  • 1,938
  • 16
  • 24
gordo
  • 11
  • 1

1 Answers1

1

When working with iFrames in jQuery, you've got to use the .contents() method.

jQuery .contents() documentation

For example. if your "clickable" element in the iFrame is "#clickme", you'd do something like this:

var ifrStuff = $('#cust_iframe_2').contents().get(0);
$(ifrStuff).on('click','#clickme',function () {
    alert('found');
});
animuson
  • 53,861
  • 28
  • 137
  • 147
DevlshOne
  • 8,357
  • 1
  • 29
  • 37
  • Thank you for the quick response. So far I'm not having any luck with this suggestion within the confines of how I am able to add the javascript. I'll try to accomplish the same thing with a Chrome extension so I can see if I'm just limited by the web application itself. – gordo Aug 22 '13 at 15:59