Skip to content

Commit 1d5f6ed

Browse files
Elchi3hamishwillee
andauthored
HTML enumerated attributes are case-insensitive (mdn#40377)
* HTML enumerated attributes are case-insensitive * Add a sentence about IDL reflection * Document enumerated reflected attributes in DOM guide * Add a pointer * Update files/en-us/glossary/enumerated/index.md Co-authored-by: Hamish Willee <hamishwillee@gmail.com> --------- Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
1 parent 09109b6 commit 1d5f6ed

File tree

2 files changed

+32
-1
lines changed
  • files/en-us
    • glossary/enumerated
    • web/api/document_object_model/reflected_attributes

2 files changed

+32
-1
lines changed

files/en-us/glossary/enumerated/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In computer science, an **enumerated** type is a data type consisting of a limit
99

1010
## HTML enumerated attributes
1111

12-
In HTML, [enumerated attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#enumerated-attribute) are attributes with a limited, predefined set of text values. For example, the global HTML [`dir`](/en-US/docs/Web/HTML/Reference/Global_attributes/dir) attribute has three valid values: `ltr`, `rtl`, and `auto`.
12+
In HTML, [enumerated attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#enumerated-attribute) are attributes with a limited, predefined set of text values. For example, the global HTML [`dir`](/en-US/docs/Web/HTML/Reference/Global_attributes/dir) attribute has three valid values: `ltr`, `rtl`, and `auto`. Like for HTML tag names, HTML enumerated attributes and their values are case-insensitive, so `LTR`, `RTL`, and `AUTO` will also work. The IDL-reflected property, {{domxref("HTMLElement.dir")}}, can also be set using a case-insensitive value, but will always return the canonical format of the value defined in the specification (lowercased values in this example). See [Attribute reflection](/en-US/docs/Web/API/Document_Object_Model/Reflected_attributes) for more information.
1313

1414
Each enumerated attribute has a default value for when the attribute is present without a value (the value is missing), and a default value for when the attribute is assigned an invalid value. Unlike {{Glossary("Boolean/HTML", "Boolean attribute")}} HTML attributes — which are always true when the attribute is present whether the value is present, omitted, or invalid — with enumerated HTML attributes, the default for an omitted value may be different from the default for invalid values. For example, the global HTML [`contenteditable`](/en-US/docs/Web/HTML/Reference/Global_attributes/contenteditable) attribute has two valid keywords: `true` and `false`. If the attribute is present but no value is set, the value is `true`. If a value is set, but is invalid, such as `contenteditable="contenteditable"`, the value maps to a third state, `inherit`.
1515

@@ -28,3 +28,4 @@ In JavaScript, enumerable properties are those properties whose internal enumera
2828
- {{Glossary("Boolean")}}
2929
- [JavaScript data types and data structures](/en-US/docs/Web/JavaScript/Guide/Data_structures)
3030
- [enumerated attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#enumerated-attribute) in the HTML Standard
31+
- [Attribute reflection](/en-US/docs/Web/API/Document_Object_Model/Reflected_attributes)

files/en-us/web/api/document_object_model/reflected_attributes/index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,36 @@ The {{domxref("Element.getAttribute()")}} will return `""` if the input is check
7777
The corresponding {{domxref("HTMLInputElement.checked")}} property returns `true` or `false` for the checked state.
7878
Otherwise boolean reflected attributes are the same as any other reflected attributes.
7979

80+
### Enumerated reflected attributes
81+
82+
In HTML, [enumerated attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#enumerated-attribute) are attributes with a limited, predefined set of text values. For example, the global HTML [`dir`](/en-US/docs/Web/HTML/Reference/Global_attributes/dir) attribute has three valid values: `ltr`, `rtl`, and `auto`.
83+
84+
```html
85+
<p dir="rtl">Right to left</p>
86+
```
87+
88+
Like for HTML tag names, HTML enumerated attributes and their values are case-insensitive, so `LTR`, `RTL`, and `AUTO` will also work.
89+
90+
```html
91+
<p dir="RTL">Right to left</p>
92+
```
93+
94+
The IDL-reflected property, {{domxref("HTMLElement.dir")}}, always returns a canonical value as provided in the specification (lowercased values in this example). Setting the value also serializes it to the canonical form.
95+
96+
```js
97+
const pElement = document.querySelector("p");
98+
console.log(pElement.dir); // "rtl"
99+
pElement.dir = "RTL";
100+
console.log(pElement.dir); // "rtl"
101+
```
102+
103+
Alternatively, you can use the {{domxref("Element.getAttribute()","getAttribute()")}} method of the {{domxref("Element")}} interface. It will get the attribute value from HTML without modifications.
104+
105+
```js
106+
const pElement = document.querySelector("p");
107+
console.log(pElement.getAttribute("dir"); // "RTL"
108+
```
109+
80110
## Reflected element references
81111
82112
> [!NOTE]

0 commit comments

Comments
 (0)