aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/api/domtokenlist/index.html
blob: a3b7c6fd97d6c67eae3851946161a3b581e8b399 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: DOMTokenList
slug: Web/API/DOMTokenList
tags:
  - API
  - DOM
  - DOMTokenList
  - Interface
  - Reference
translation_of: Web/API/DOMTokenList
---
<div>{{APIRef("DOM")}}</div>

<p>The <code><strong>DOMTokenList</strong></code> interface represents a set of space-separated tokens. Such a set is returned by {{domxref("Element.classList")}}, {{domxref("HTMLLinkElement.relList")}}, {{domxref("HTMLAnchorElement.relList")}}, {{domxref("HTMLAreaElement.relList")}}, {{domxref("HTMLIframeElement.sandbox")}}, or {{domxref("HTMLOutputElement.htmlFor")}}. It is indexed beginning with <code>0</code> as with JavaScript {{jsxref("Array")}} objects. <code>DOMTokenList</code> is always case-sensitive.</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("DOMTokenList.length")}} {{ReadOnlyInline}}</dt>
 <dd>Is an <code>integer</code> representing the number of objects stored in the object.</dd>
 <dt>{{domxref("DOMTokenList.value")}}</dt>
 <dd>The value of the list as a {{domxref("DOMString")}}.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("DOMTokenList.item()")}}</dt>
 <dd>Returns an item in the list by its index (returns <code>undefined</code> if the number is greater than or equal to the length of the list).</dd>
 <dt>{{domxref("DOMTokenList.contains()")}}</dt>
 <dd>Returns <code>true</code> if the list contains the given <em>token</em>, otherwise <code>false</code>.</dd>
 <dt>{{domxref("DOMTokenList.add()")}}</dt>
 <dd>Adds the given <em>token</em> to the list.</dd>
 <dt>{{domxref("DOMTokenList.remove()")}}</dt>
 <dd>Removes the specified <em>token</em> from the list.</dd>
 <dt>{{domxref("DOMTokenList.replace()")}}</dt>
 <dd>Replaces an existing <em>token</em> with a new token.</dd>
 <dt>{{domxref("DOMTokenList.supports()")}}</dt>
 <dd>Returns <code>true</code> if a given <em>token</em> is in the associated attribute's supported tokens.</dd>
 <dt>{{domxref("DOMTokenList.toggle()")}}</dt>
 <dd>Removes a given <em>token</em> from the list and returns <code>false</code>. If <em>token</em> doesn't exist it's added and the function returns <code>true</code>.</dd>
 <dt>{{domxref("DOMTokenList.entries()")}}</dt>
 <dd>Returns an {{jsxref("Iteration_protocols","iterator")}} allowing you to go through all key/value pairs contained in this object.</dd>
 <dt>{{domxref("DOMTokenList.forEach()")}}</dt>
 <dd>Executes a provided function once per <code>DOMTokenList</code> element.</dd>
 <dt>{{domxref("DOMTokenList.keys()")}}</dt>
 <dd>Returns an {{jsxref("Iteration_protocols", "iterator")}} allowing you to go through all keys of the key/value pairs contained in this object.</dd>
 <dt>{{domxref("DOMTokenList.values()")}}</dt>
 <dd>Returns an {{jsxref("Iteration_protocols", "iterator")}} allowing you to go through all values of the key/value pairs contained in this object.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<p>In the following simple example we retrieve the list of classes set on a {{htmlelement("p")}} element as a <code>DOMTokenList</code> using {{domxref("Element.classList")}}, add a class using {{domxref("DOMTokenList.add()")}}, and then update the {{domxref("Node.textContent")}} of the <code>&lt;p&gt;</code> to equal the <code>DOMTokenList</code>.</p>

<p>First, the HTML:</p>

<pre class="brush: html">&lt;p class="a b c"&gt;&lt;/p&gt;</pre>

<p>Now the JavaScript:</p>

<pre class="brush: js">var para = document.querySelector("p");
var classes = para.classList;
para.classList.add("d");
para.textContent = 'paragraph classList is "' + classes + '"';</pre>

<p>The output looks like this:</p>

<p>{{ EmbedLiveSample('Examples', '100%', 60) }}</p>

<h2 id="Trimming_of_whitespace_and_removal_of_duplicates">Trimming of whitespace and removal of duplicates</h2>

<p>Methods that modify the <code>DOMTokenList</code> (such as {{domxref("DOMTokenList.add()")}}) automatically trim any excess {{Glossary("Whitespace")}} and remove duplicate values from the list. For example:</p>

<pre class="brush: html">&lt;span class="    d   d e f"&gt;&lt;/span&gt;</pre>

<pre class="brush: js">var span = document.querySelector("span");
var classes = span.classList;
span.classList.add("x");
span.textContent = 'span classList is "' + classes + '"';</pre>

<p>The output looks like this:</p>

<p>{{ EmbedLiveSample('Trimming_of_whitespace_and_removal_of_duplicates', '100%', 60) }}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("DOM WHATWG", "#interface-domtokenlist", "DOMTokenList")}}</td>
   <td>{{Spec2("DOM WHATWG")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.DOMTokenList")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("DOMSettableTokenList")}} (object that extends <code>DOMTokenList</code> with settable <em>.value</em> property)</li>
</ul>