Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New page: HTMLInputElement.list #35625

Merged
merged 7 commits into from
Aug 28, 2024
61 changes: 61 additions & 0 deletions files/en-us/web/api/htmlinputelement/list/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
title: "HTMLInputElement: list property"
short-title: list
slug: Web/API/HTMLInputElement/list
page-type: web-api-instance-property
browser-compat: api.HTMLInputElement.list
---

{{ApiRef("HTML DOM")}}

The **`list`** read-only property of the {{domxref("HTMLInputElement")}} interface returns the {{domxref("HTMLDataListElement")}} pointed to by the [`list`](/en-US/docs/Web/HTML/Element/input#list) attribute of the element, or `null` if the `list` attribute is not defined or the `list` attribute's value is not associated with any `<datalist>` in the same tree.

The `list` attribute identifies a `<datalist>` in the same tree providing predefined options to the user.
estelle marked this conversation as resolved.
Show resolved Hide resolved
estelle marked this conversation as resolved.
Show resolved Hide resolved

> [!NOTE]
> This is a read-only property. To associate a `<datalist>` with an element, set the value of the `list` attribute with {{domxref("Element.setAttribute", "setAttribute()")}}.

## Value

An {{domxref("HTMLDataListElement")}} or `null`.

## Example

Given the following HTML:

```html
<label for="planet">Which planet are you from?</label>
<input id="planet" type="text" list="superhero" />
<datalist id="superhero">
<option value="Azarath" />
<option value="Krypton" />
<option value="Tamaran" />
</datalist>
</datalist>
estelle marked this conversation as resolved.
Show resolved Hide resolved
```

You can retrieve the `<datalist>` element associated with the `<input>`:

```js
const inputElement = document.querySelector("#planet");
console.log(inputElement.list); // returns the superhero HTMLDatalistElement
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- {{domxref("HTMLInputElement.value")}}
- {{domxref("HTMLInputElement.type")}}
- {{domxref("HTMLDataListElement")}}
- {{domxref("HTMLOptionElement")}}
- {{domxref("HTMLCollection")}}
- {{htmlelement("input")}}
- {{htmlelement("datalist")}}
- {{htmlelement("option")}}
Loading