Skip to content

Commit 13a3c24

Browse files
Reword HTMLCollection.namedItem replacement (#35635)
* Remove incorrect information about HTMLCollection In JavaScript, all non-Symbol property key values (including index numbers) are converted to Strings, so there is no way for the language to differentiate between `collection[1]` and `collection["1"]`. That means that `namedItem` and computed property access cannot be equivalent since `namedItem` doesn't return elements by index while computed property access does. * Update index.md --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 592bea2 commit 13a3c24

File tree

1 file changed

+4
-3
lines changed
  • files/en-us/web/api/htmlcollection/nameditem

1 file changed

+4
-3
lines changed

files/en-us/web/api/htmlcollection/nameditem/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ browser-compat: api.HTMLCollection.namedItem
1111
The **`namedItem()`** method of the {{domxref("HTMLCollection")}} interface returns
1212
the first {{domxref("Element")}} in the collection whose `id` or `name` attribute match the specified name, or `null` if no element matches.
1313

14-
In JavaScript, using the array bracket syntax with a {{jsxref("String")}}, like `collection["value"]` is equivalent to `collection.namedItem("value")`.
14+
In JavaScript, instead of calling `collection.namedItem("value")`, you can also directly access the name on the collection, like `collection["value"]`, unless the name collides with one of the existing `HTMLCollection` properties.
1515

1616
## Syntax
1717

@@ -21,11 +21,12 @@ namedItem(key)
2121

2222
### Parameters
2323

24-
- `key` is a string representing the value of the `id` or `name` attribute of the element we are looking for.
24+
- `key`
25+
- : A string representing the value of the `id` or `name` attribute of the element we are looking for.
2526

2627
### Return value
2728

28-
- `item` is the first {{domxref("Element")}} in the {{domxref("HTMLCollection")}} matching the _key_, or [`null`](/en-US/docs/Web/JavaScript/Reference/Operators/null), if there are none.
29+
The first {{domxref("Element")}} in the {{domxref("HTMLCollection")}} matching the `key`, or [`null`](/en-US/docs/Web/JavaScript/Reference/Operators/null) if there is none. Always returns `null` if `key` is the empty string.
2930

3031
## Example
3132

0 commit comments

Comments
 (0)