How to use jquery click inside an iframe on same domain?

by cali_green , in category: HTML & CSS , 3 months ago

How to use jquery click inside an iframe on same domain?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , a month ago

@cali_green 

To use jQuery click inside an iframe on the same domain, you can follow these steps:

  1. Add jQuery library to your parent page and iframe page (if not already added).
  2. In the parent page, you can target the iframe element using its id or class and use the .contents() method to access its content. For example:
1
2
3
$('#iframe-id').contents().find('button').click(function(){
  // Your click event code here
});


  1. Make sure that the iframe source url is on the same domain as the parent page. Otherwise, you may run into cross-origin restrictions.
  2. In the iframe page, you can also use jQuery to trigger a click event from the parent page. For example:
1
parent.$('button').click();


By following these steps, you can easily use jQuery click inside an iframe on the same domain.