Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The assign() method of the HTMLSlotElement interface sets the slot's manually assigned nodes to an ordered set of slottables. The manually assigned nodes set is initially empty until nodes are assigned using assign().
Note: You cannot mix manually (imperative) and named (declarative, automatic) slot assignments. Therefore, for this method to work, the shadow tree needs to have been created with the slotAssignment: "manual" option.
assign(node1) assign(node1, node2) assign(node1, node2, /* …, */ nodeN)
None (undefined).
NotAllowedError DOMException
Thrown when calling this method on an automatically assigned slot.
In the example below, the assign() method is used to display the correct tab in a tabbed application. The function is called and passed the panel to show, which is then assigned to the slot.
function UpdateDisplayTab(elem, tabIdx) {
const shadow = elem.shadowRoot;
const slot = shadow.querySelector("slot");
const panels = elem.querySelectorAll("tab-panel");
if (panels.length && tabIdx && tabIdx <= panels.length) {
slot.assign(panels[tabIdx - 1]);
} else {
slot.assign();
}
}
| Specification |
|---|
| HTML> # dom-slot-assign> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
assign |
92Before Chrome 95, the method accepted anyNode instead of just Element and Text.86–92Acceptedsequence<Node> instead of (Element or Text).... |
92Before Edge 95, the method accepted anyNode instead of just Element and Text.86–92Acceptedsequence<Node> instead of (Element or Text).... |
92 |
78Before Opera 81, the method accepted anyNode instead of just Element and Text.72–78Acceptedsequence<Node> instead of (Element or Text).... |
16.4 |
92Before Chrome Android 95, the method accepted anyNode instead of just Element and Text.86–92Acceptedsequence<Node> instead of (Element or Text).... |
92 |
65Before Opera Android 67, the method accepted anyNode instead of just Element and Text.61–65Acceptedsequence<Node> instead of (Element or Text).... |
16.4 |
16.0Before Samsung Internet 17.0, the method accepted anyNode instead of just Element and Text.14.0–16.0Acceptedsequence<Node> instead of (Element or Text).... |
92Before WebView Android 95, the method accepted anyNode instead of just Element and Text.86–92Acceptedsequence<Node> instead of (Element or Text).... |
16.4 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSlotElement/assign