Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  49] [ 4]  / answers: 1 / hits: 17528  / 10 Years ago, sun, april 6, 2014, 12:00:00

I'm trying to fire a click event on the click of an element inside an iframe, but it doesn't seem to be working. My current set up:

jsFiddle: http://jsfiddle.net/q4aa3/

jQuery:



$(document).ready(function () {
$('#this-iframe').load(function () {
$('#this-iframe').contents().find('img').live({
click: function () {
alert('clicked img');
}
});
});
});


Clicking on the image inside the iframe isn't firing the alert, I'm not sure why, or is there a better way to achieve this? Any suggestions would be greatly appreciated!


More From » jquery

 Answers
10

When you have the iframe on the same domain, you can use this script to catch clicks in the iframe. Don't use .live, it is depriciated as of jQuery 1.7.



var iframeBody = $('body', $('#iframe')[0].contentWindow.document);
$(iframeBody).on('click', 'img', function(event) {
doSomething();
});


You can manipulate the body through the iframeBody variable.


[#71589] Friday, April 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jadap

Total Points: 101
Total Questions: 104
Total Answers: 98

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;