diff options
Diffstat (limited to 'devdocs/html/global_attributes%2Fid.html')
| -rw-r--r-- | devdocs/html/global_attributes%2Fid.html | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/devdocs/html/global_attributes%2Fid.html b/devdocs/html/global_attributes%2Fid.html new file mode 100644 index 00000000..4c25a489 --- /dev/null +++ b/devdocs/html/global_attributes%2Fid.html @@ -0,0 +1,61 @@ +<header><h1>id</h1></header><div class="section-content"><p>The <code>id</code> <a href="../global_attributes">global attribute</a> defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#fragment">fragment identifier</a>), scripting, or styling (with <a href="https://developer.mozilla.org/en-US/docs/Glossary/CSS">CSS</a>).</p></div> +<h2 id="try_it">Try it</h2> +<div class="section-content"> +<iframe class="interactive is-tabbed-shorter-height" height="200" src="https://interactive-examples.mdn.mozilla.net/pages/tabbed/attribute-id.html" title="MDN Web Docs Interactive Example" loading="lazy"></iframe> <div class="notecard warning" id="sect1"> <p><strong>Warning:</strong> This attribute's value is an opaque string: this means that web authors should not rely on it to convey human-readable information (although having your IDs somewhat human-readable can be useful for code comprehension, e.g. consider <code>ticket-18659</code> versus <code>r45tgfe-freds&$@</code>).</p> </div> <p>An <code>id</code>'s value must not contain <a href="https://developer.mozilla.org/en-US/docs/Glossary/Whitespace">whitespace</a> (spaces, tabs, etc.). Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the <a href="../global_attributes#class"><code>class</code></a> attribute, which allows space-separated values, elements can only have one single ID value.</p> <p>Technically, the value for an <code>id</code> attribute may contain any character, except <a href="https://developer.mozilla.org/en-US/docs/Glossary/Whitespace">whitespace</a> characters. However, to avoid inadvertent errors, only <a href="https://developer.mozilla.org/en-US/docs/Glossary/ASCII">ASCII</a> letters, digits, <code>'_'</code>, and <code>'-'</code> should be used, and the value for an <code>id</code> attribute should start with a letter.</p> <p>For example, <code>.</code> has a special meaning in CSS (it starts a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors">class selector</a>). While valid, unless you are careful to escape it when used as part of a CSS selector, it won't be recognized as part of the element's <code>id</code>. The same applies to the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector"><code>querySelector()</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll"><code>querySelectorAll()</code></a> parameters, which use the same selector syntax. It is easy to forget to do this, resulting in bugs in your code that could be hard to detect.</p> <p>Similarly, an <code>id</code> starting with a digit (E.g., <code>1234-322-678</code>) or a hyphen followed by a digit (E.g., <code>-123</code>), though valid in HTML, may lead to problems when used in CSS, JavaScript, and Web APIs:</p> <ul> <li>CSS <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors">ID selectors</a> accept any CSS identifier. If the <code>id</code> starts with a digit or one hyphen immediately followed by a digit, both the hyphen and digit must be escaped in CSS. For example, while <code>id="544-383-3388"</code> and <code>id="-3Pi"</code> are valid in HTML, the <code>id</code> selectors must be escaped. The element with these <code>id</code> values can be targeted in CSS with <code>#\35 44-383-3388</code> and <code>#\2D \33 pi</code>.</li> <li>Any valid HTML <code>id</code> value is valid as an attribute selector in CSS and JavaScript. The selectors <code>[id="544-383-3388"]</code> and <code>[id="-3Pi"]</code> are valid.</li> <li> +<a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector"><code>Document.querySelector()</code></a> and similar methods using CSS-selector-style queries will not find them unless you escape them. (See this <a href="https://codepen.io/estelle/pen/jOvzbgb" target="_blank">page</a> for an example.)</li> <li>Such an <code>id</code> is not a valid JavaScript identifier. <a href="https://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-properties" target="_blank">Elements with IDs become global properties</a>, but you cannot use non-identifier global properties as global variables — <code>1234</code> is not a global variable, and you have to use <code>window[1234]</code> instead to get the element with <code>id="1234"</code>. This is slightly inconvenient as you have to create the variable with an extra step: <code>const element = window[1234]</code>.</li> </ul> +</div> +<h2 id="specifications">Specifications</h2> +<div class="_table"><table class="standard-table"> +<thead><tr><th scope="col">Specification</th></tr></thead> +<tbody><tr><td><a href="https://html.spec.whatwg.org/multipage/dom.html#global-attributes:the-id-attribute-2">HTML Standard <br><small># global-attributes:the-id-attribute-2</small></a></td></tr></tbody> +</table></div> +<h2 id="browser_compatibility">Browser compatibility</h2> +<div class="_table"><table> +<thead> +<tr id="bct-browser-type"> +<th></th> +<th colspan="6">Desktop</th> +<th colspan="6">Mobile</th> +</tr> +<tr id="bct-browsers"> +<th></th> +<th>Chrome</th> +<th>Edge</th> +<th>Firefox</th> +<th>Internet Explorer</th> +<th>Opera</th> +<th>Safari</th> +<th>WebView Android</th> +<th>Chrome Android</th> +<th>Firefox for Android</th> +<th>Opera Android</th> +<th>Safari on IOS</th> +<th>Samsung Internet</th> +</tr> +</thead> +<tbody><tr> +<th><code>id</code></th> +<td class="bc-supports-yes">1</td> +<td class="bc-supports-yes">12</td> +<td class="bc-supports-yes">32<details><summary>1–32</summary><code>id</code> is a true global attribute only since Firefox 32.</details> +</td> +<td class="bc-supports-yes">Yes</td> +<td class="bc-supports-yes">15</td> +<td class="bc-supports-yes">1</td> +<td class="bc-supports-yes">4.4</td> +<td class="bc-supports-yes">18</td> +<td class="bc-supports-yes">32<details><summary>4–32</summary><code>id</code> is a true global attribute only since Firefox 32.</details> +</td> +<td class="bc-supports-yes">14</td> +<td class="bc-supports-yes">1</td> +<td class="bc-supports-yes">1.0</td> +</tr></tbody> +</table></div> +<h2 id="see_also">See also</h2> +<div class="section-content"><ul> <li>All <a href="../global_attributes">global attributes</a>.</li> <li> +<a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/id"><code>Element.id</code></a> that reflects this attribute.</li> <li>The <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById"><code>Document.getElementById</code></a> method.</li> <li>CSS <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors">ID selectors</a>.</li> </ul></div><div class="_attribution"> + <p class="_attribution-p"> + © 2005–2023 MDN contributors.<br>Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.<br> + <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id" class="_attribution-link">https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id</a> + </p> +</div> |
