Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

88
Views
How to pass Javascript custom event past the shadowDOM

I'm writing a Javascript custom element and want a event to bubbel past it's shadowDOM boundary. Catching the event inside the shadowDOM is no problem but how can I listen for the event in the lightDOM?

html

    <div class="tasks">
        <m-task name="Abc" number=123></m-task>
    </div>

javascript

customElements.define('m-task',
    class mTask extends HTMLElement {
        constructor() {
            super()
            this.attachShadow({ mode: 'open' })
        

        // ---> LISTENING FOR EVENT <---
        this.addEventListener('connected', e => {
                console.log(e.detail)
            })
        // ---> LISTENING FOR EVENT <---
        this.addEventListener('disconnected', e => {
                console.log(e.detail)
            })

        }
        connectedCallback() {
            console.log('connected', this)
            this.dispatchEvent(new CustomEvent('connected', {
                bubbles: true,
                composed: true,
                detail: {
                    type: 'connected',
                    value: 1234
                }
            }))
        }
        disconnectedCallback() {
            console.log('disconnected', this)
            this.dispatchEvent(new CustomEvent('disconnected', {
                bubbles: true,
                composed: true,
                detail: {
                    type: 'disconnected',
                    value: -99
                }
            }))
        }
    })

// ---> LISTENING FOR EVENT <---
//      THIS IS NOT WORKING
document.querySelector('.tasks').addEventListener('connected', e => {
    console.log('listening in the light')
    console.log(e.detail)
})

console

connected <m-task name="Abc" number="123">
Object { type: "connected", value: 1234 }
disconnected <m-task name="Abc" number="123">
Object { type: "disconnected", value: -99 }

edit

waiting 1ms gives the expected result

setTimeout(() => {
    this.dispatchEvent(new CustomEvent('connected', {
        bubbles: true,
        composed: true,
        detail: {
            type: 'connected',
            value: 1234
        }
     }))
}, 1);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!