blob: 2644171f01055eb9537e17d7fb05bbb92a7d0121 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
---
title: HTMLSlotElement
slug: Web/API/HTMLSlotElement
tags:
- API
- HTMLSlotElement
- Interface
- NeedsTranslation
- Reference
- TopicStub
- shadow dom
translation_of: Web/API/HTMLSlotElement
---
<p>{{APIRef('Web Components')}}</p>
<p>The <strong><code>HTMLSlotElement</code></strong> interface of the <a href="/en-US/docs/Web/Web_Components/Shadow_DOM">Shadow DOM API</a> enables access to the name and assigned nodes of an HTML {{HTMLElement("slot")}} element.</p>
<h2 id="Properties">Properties</h2>
<dl>
<dt>{{domxref('HTMLSlotElement.name')}}</dt>
<dd>{{domxref("DOMString")}}: Can be used to get and set the slot's name.</dd>
</dl>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref('HTMLSlotElement.assignedNodes()')}}</dt>
<dd>Returns the sequence of elements assigned to this slot, or alternatively the slot's fallback content.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<p>The following snippet is taken from our <a href="https://github.com/mdn/web-components-examples/tree/master/slotchange">slotchange example</a> (<a href="https://mdn.github.io/web-components-examples/slotchange/">see it live also</a>).</p>
<pre class="brush: js">let slots = this.shadowRoot.querySelectorAll('slot');
slots[1].addEventListener('slotchange', function(e) {
let nodes = slots[1].assignedNodes();
console.log('Element in Slot "' + slots[1].name + '" changed to "' + nodes[0].outerHTML + '".');
});</pre>
<p>Here we grab references to all the slots, then add a slotchange event listener to the 2nd slot in the template — which is the one that keeps having its contents changed in the example.</p>
<p>Every time the element inserted in the slot changes, we log a report to the console saying which slot has changed, and what the new node inside the slot is.</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('HTML WHATWG','scripting.html#htmlslotelement','HTMLSlotElement')}}</td>
<td>{{Spec2('HTML WHATWG')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.HTMLSlotElement")}}</p>
|