HTMLSlotElement.assign()

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().

Please note that you cannot mix declarative and imperative slot assignment. Therefore, for this to work, the shadow tree needs to have been created with the slotAssignment: "manual" option.

Syntax

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

Parameters

node1, …, nodeN

A set of Element or Text nodes.

Return value

Undefined.

Examples

In the below example, 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();
  }
}

Specifications

Specification
HTML Standard
# dom-slot-assign

Browser compatibility

BCD tables only load in the browser