简体   繁体   中英

Listen to custom events from another custom elements

I have two Javascript custom elements (let's call them source and destination). Source triggers a custom event that bubbles up fine to document and I can catch it there with an event listener. If I add an event listener to a div in my destination custom element the same event is not captured. Is that correct or should I be able to catch a custom event from another custom element?

elAuthorSelect.dispatchEvent(new window.CustomEvent('authorChanged', {
        detail: { name: 'test' },
        bubbles: true,
        cancelable: false,
        composed: true

      })

I think I've misunderstood bubbling. It seems as can been read in this link https://javascript.info/bubbling-and-capturing under the heading Capture that bubbling/capture only travel up/down it's own branch and not to other branches in the dom. I thought of dispatchevent as more of a broadcast thingy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM