HTMLSlotElement: assign() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2023.

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.

Syntax

js
assign(node1)
assign(node1, node2)
assign(node1, node2, /* …, */ nodeN)

Parameters

node1, …, nodeN

A set of Element or Text nodes.

Return value

None (undefined).

Exceptions

NotAllowedError DOMException

Thrown when calling this method on an automatically assigned slot.

Examples

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.

js
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();
  }
}

Specifications

Specification
HTML
# dom-slot-assign

Browser compatibility

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
Chrome – Partial support
Chrome 86 – 91 (Release date: 2020-10-20)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
Chrome – Full support
Chrome 92 (Release date: 2021-07-20)
footnote
footnote Before Chrome 95, the method accepted any Node instead of just Element and Text.
Edge – Partial support
Edge 86 – 91 (Release date: 2020-10-09)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
Edge – Full support
Edge 92 (Release date: 2021-07-22)
footnote
footnote Before Edge 95, the method accepted any Node instead of just Element and Text.
Firefox – Full support
Firefox 92 (Release date: 2021-09-07)
footnote Full support
Opera – Partial support
Opera 72 – 77 (Release date: 2020-10-21)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
Opera – Full support
Opera 78 (Release date: 2021-08-03)
footnote
footnote Before Opera 81, the method accepted any Node instead of just Element and Text.
Safari – Full support
Safari 16.4 (Release date: 2023-03-27)
footnote Full support
Chrome Android – Partial support
Chrome Android 86 – 91 (Release date: 2020-10-20)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
Chrome Android – Full support
Chrome Android 92 (Release date: 2021-07-20)
footnote
footnote Before Chrome Android 95, the method accepted any Node instead of just Element and Text.
Firefox for Android – Full support
Firefox for Android 92 (Release date: 2021-09-07)
footnote Full support
Opera Android – Partial support
Opera Android 61 – 64 (Release date: 2020-12-07)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
Opera Android – Full support
Opera Android 65 (Release date: 2021-10-20)
footnote
footnote Before Opera Android 67, the method accepted any Node instead of just Element and Text.
Safari on iOS – Full support
Safari on iOS 16.4 (Release date: 2023-03-27)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 14 – 15 (Release date: 2021-04-17)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
Samsung Internet – Full support
Samsung Internet 16 (Release date: 2021-11-25)
footnote
footnote Before Samsung Internet 17.0, the method accepted any Node instead of just Element and Text.
WebView Android – Partial support
WebView Android 86 – 91 (Release date: 2020-10-20)
footnote Partial support
footnote Accepted sequence<Node> instead of (Element or Text)....
WebView Android – Full support
WebView Android 92 (Release date: 2021-07-20)
footnote
footnote Before WebView Android 95, the method accepted any Node instead of just Element and Text.
WebView on iOS – Full support
WebView on iOS 16.4 (Release date: 2023-03-27)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
See implementation notes.
Has more compatibility info.

See also