🔹`slotchange` event
JS.info ⟩ Updating slots
Google ⟩ Working with slots in JS
MDN ⟩ MutationObserver
MDN ⟩
HTMLSlotElement ⟩ assignedNodes(): Node[ ]
Element ⟩ assignedSlot: HTMLSlotElement - slot that element is assigned to.
The slotchange event fires when a slot's distributed nodes changes. For example, if the user adds/removes children from the light DOM.
const slot = this.shadowRoot.querySelector('#slot');
// ⭐️ `slotchange` event listener
slot.addEventListener('slotchange', e => {
console.log('light dom children changed!');
});example
💾 replit
slot name
notes
title
<span slot="title">Candy menu</span> inserted into title slot.
item
new <li slot="item"> tag inserted into item slot.
item
new <li slot="item"> tag inserted into item slot.
item
new <li slot="item"> tag inserted into item slot.
there's NOslotchange event after 4 sec (on modifying the content of a slotted element).
Last updated
Was this helpful?