diff --git a/files/en-us/web/html/global_attributes/class/index.md b/files/en-us/web/html/global_attributes/class/index.md index fd6df31c4499029..598d60efe2e7c29 100644 --- a/files/en-us/web/html/global_attributes/class/index.md +++ b/files/en-us/web/html/global_attributes/class/index.md @@ -23,6 +23,8 @@ The `class` attribute is a list of class values separated by [ACSII whitespace]( Each class value may contain any Unicode characters (except, of course, ASCII whitespace). However, when used in CSS selectors, either from JavaScript using APIs like {{domxref("Document.querySelector()")}} or in CSS stylesheets, class attribute values must be valid [CSS identifiers](/en-US/docs/Web/CSS/ident). This means that if a class attribute value is not a valid CSS identifier (for example, `my?class` or `1234`) then it must be escaped before being used in a selector, either using the {{domxref("CSS.escape_static", "CSS.escape()")}} method or [manually](/en-US/docs/Web/CSS/ident#escaping_characters). +For this reason, it's recommended that developers choose values for class attributes that are valid CSS identifiers. + ## Specifications {{Specifications}} diff --git a/files/en-us/web/html/global_attributes/id/index.md b/files/en-us/web/html/global_attributes/id/index.md index 4a0b442c524aae0..eeb21a327b2b8b8 100644 --- a/files/en-us/web/html/global_attributes/id/index.md +++ b/files/en-us/web/html/global_attributes/id/index.md @@ -13,9 +13,9 @@ The **`id`** [global attribute](/en-US/docs/Web/HTML/Global_attributes) defines ## Description -The purpose of the `id` attribute is to identify a single element when linking (using a [fragment identifier](/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#fragment)), scripting, or styling (with {{glossary("CSS")}}). +The purpose of the ID attribute is to identify a single element when linking (using a [fragment identifier](/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#fragment)), scripting, or styling (with {{glossary("CSS")}}). -Elements with `id` attributes are available as global properties. The property name is the `id` attribute, and the property value is the element. For example, given markup like: +Elements with ID attributes are available as global properties. The property name is the ID attribute, and the property value is the element. For example, given markup like: ```html
@@ -29,11 +29,13 @@ const content = window.preamble.textContent; ### Syntax -An `id`'s value must not contain [ACSII whitespace](/en-US/docs/Glossary/Whitespace#in_html) characters. Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the [`class`](/en-US/docs/Web/HTML/Global_attributes#class) attribute, which allows space-separated values, elements can only have one single ID value. +An ID attribute's value must not contain [ACSII whitespace](/en-US/docs/Glossary/Whitespace#in_html) characters. Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the [`class`](/en-US/docs/Web/HTML/Global_attributes#class) attribute, which allows space-separated values, elements can only have one single ID value. -Technically, the value for an `id` attribute may contain any other Unicode character. However, when used in CSS selectors, either from JavaScript using APIs like {{domxref("Document.querySelector()")}} or in CSS stylesheets, ID attribute values must be valid [CSS identifiers](/en-US/docs/Web/CSS/ident). This means that if an ID attribute value is not a valid CSS identifier (for example, `my?id` or `1234`) then it must be escaped before being used in a selector, either using the {{domxref("CSS.escape_static", "CSS.escape()")}} method or [manually](/en-US/docs/Web/CSS/ident#escaping_characters). +Technically, the value for an ID attribute may contain any other Unicode character. However, when used in CSS selectors, either from JavaScript using APIs like {{domxref("Document.querySelector()")}} or in CSS stylesheets, ID attribute values must be valid [CSS identifiers](/en-US/docs/Web/CSS/ident). This means that if an ID attribute value is not a valid CSS identifier (for example, `my?id` or `1234`) then it must be escaped before being used in a selector, either using the {{domxref("CSS.escape_static", "CSS.escape()")}} method or [manually](/en-US/docs/Web/CSS/ident#escaping_characters). -Also, not all valid `id` attribute values are valid JavaScript identifiers. For example, `1234` is a valid attribute value but not a valid JavaScript identifier. This means that the value is not a valid variable name, so you can't access the element using code like `window.1234`. However, you can access it using `window["1234"]`. +For this reason, it's recommended that developers choose values for ID attributes that are valid CSS identifiers. + +Also, not all valid ID attribute values are valid JavaScript identifiers. For example, `1234` is a valid attribute value but not a valid JavaScript identifier. This means that the value is not a valid variable name, so you can't access the element using code like `window.1234`. However, you can access it using `window["1234"]`. ## Specifications