How to Detect a Click from an Iframe for a Specific Div? Need Help!

16 Replies, 1520 Views

Hey everyone,

So I’ve been stuck on this issue for a while now and could really use some help. I’m trying to *detect a click from iframe for specific div* on my page. Basically, there’s an iframe embedded, and I need to know when someone clicks inside it, but only if they click on a particular div within that iframe.

I’ve tried a few things like `postMessage` and event listeners, but I’m not sure if I’m doing it right. The iframe is from the same domain, so no cross-origin issues, but still, it’s driving me nuts lol.

Anyone got a working example or some tips on how to *detect a click from iframe for specific div*? Would really appreciate it!

Thanks in advance!
- Alex
Hey Alex!
I had a similar issue last month, and I managed to solve it using `postMessage`. Since your iframe is on the same domain, it should work for you too. Basically, you need to add an event listener inside the iframe to detect clicks on the specific div and then send a message to the parent window.

Here’s a quick example:
```javascript
// Inside iframe
document.getElementById('specificDiv').addEventListener('click', () => {
window.parent.postMessage('divClicked', '*');
});

// In parent window
window.addEventListener('message', (event) => {
if (event.data === 'divClicked') {
console.log('Click detected on the specific div!');
}
});
```
Hope this helps!
Yo Alex,
I feel your pain lol. Detecting a click from iframe for specific div can be tricky, but since it’s same-domain, you’re in luck. Have you tried using `contentWindow`? You can access the iframe’s DOM directly and attach an event listener to the div.

Something like this:
```javascript
const iframe = document.getElementById('yourIframe');
const iframeDoc = iframe.contentWindow.document;

iframeDoc.getElementById('specificDiv').addEventListener('click', () => {
console.log('Click detected!');
});
```
Let me know if this works for ya!
Hey there!
If you’re still struggling, you might wanna check out this tool called [iframe-resizer](https://github.com/davidjbradshaw/iframe-resizer). It’s super handy for handling iframe interactions, including detecting clicks. It even supports cross-origin iframes, but since yours is same-domain, it should be even easier to set up.

For your case, you can use their `onClick` callback to detect clicks on the specific div. Just make sure to configure it properly. Good luck!
Alex,
I’ve been down this road before, and honestly, `postMessage` is the way to go. But if you’re not getting it to work, maybe double-check your event listeners? Sometimes the issue is just a typo or a timing problem where the iframe isn’t fully loaded when you try to attach the listener.

You could also try using `MutationObserver` to wait for the iframe content to load before attaching the click handler. Just a thought!
Wow, thanks so much, everyone! I didn’t expect so many helpful replies. I tried the `postMessage` approach, and it’s working now! I was definitely missing the part where I needed to wait for the iframe to load fully before attaching the event listener.

Quick follow-up: Has anyone tried this with dynamically loaded iframes? Like, if the iframe content changes after the initial load? Would I need to reattach the event listener every time? Thanks again, you guys are awesome!
Hey!
Detecting a click from iframe for specific div is definitely doable, but it can be a bit of a headache. One thing I’d suggest is using `window.frames` to access the iframe’s content. Here’s a quick snippet:

```javascript
const iframe = window.frames['yourIframeName'];
iframe.document.getElementById('specificDiv').addEventListener('click', () => {
alert('Div clicked!');
});
```
Make sure the iframe is fully loaded before running this, though. Otherwise, it’ll throw errors.
Alex,
I’d recommend checking out [MDN’s guide on postMessage](https://developer.mozilla.org/en-US/docs...ostMessage). It’s super detailed and has examples for exactly what you’re trying to do. Since your iframe is same-domain, you shouldn’t have any issues with permissions or CORS.

Also, make sure you’re using `event.origin` in the parent window to verify the message source. Security first, ya know?
Hey Alex,
Just wanted to add that if you’re still stuck, you might wanna try using jQuery inside the iframe. It simplifies a lot of the DOM manipulation and event handling. Here’s how you can do it:

```javascript
$('#specificDiv', $('#yourIframe').contents()).click(() => {
console.log('Click detected!');
});
```
It’s not the most modern approach, but it gets the job done. Good luck!



Users browsing this thread: 1 Guest(s)