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

16 Replies, 1524 Views

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!

Messages In This Thread



Users browsing this thread: 1 Guest(s)