javascript – I need to make a click handler for an element inside an iframe

Question:

I need to make a click handler for an element inside an iframe. Tried like this but nothing works

jQuery('body').on('click', 'iframe .review-submit-button', function(e) {
 console.log('test');
});

Answer:

Here's how you can do it:

$('iframe').load(function(){
  $(this).contents().find("body").on('click', function(event) { alert('test'); });
});

An example of how the code works here .

Note: this will only work if both pages are on the same domain.

Scroll to Top