Observable Framework 1.5.1 GitHub️ 1.8k

Text input

API · Source · The text input allows freeform single-line text entry. For multiple lines, see the text area input.

In its most basic form, a text input is a blank box whose value is the empty string. The text’s value changes as the user types into the box.

const text = view(Inputs.text());

We recommend providing a label and placeholder to improve usability. You can also supply an initial value if desired.

const name = view(
    label: "Name",
    placeholder: "Enter your name",
    value: "Anonymous"

The label may be either a text string or an HTML element, if more control over styling is desired.

const signature = view(
    label: html`<b>Fancy</b>`,
    placeholder: "What’s your fancy?"

For specific classes of text, such as email addresses and telephone numbers, you can supply one of the HTML5 input types, such as email, tel (for a telephone number), or url, as the type option. Or, use a convenience method: Inputs.email, Inputs.password, Inputs.tel, or Inputs.url.

const password = view(Inputs.password({label: "Password", value: "open sesame"}));

The HTML5 pattern, spellcheck, minlength, and maxlength options are also supported. If the user enters invalid input, the browser may display a warning (e.g., “Enter an email address”).

const email = view(
    type: "email",
    label: "Email",
    placeholder: "Enter your email"

If the input will trigger some expensive calculation, such as fetching from a remote server, the submit option can be used to defer the text input from reporting the new value until the user clicks the Submit button or hits Enter. The value of submit can also be the desired contents of the submit button (a string or HTML).

const query = view(Inputs.text({label: "Query", placeholder: "Search", submit: true}));

To provide a recommended set of values, pass an array of strings as the datalist option. For example, the input below is intended to accept the name of a U.S. state; you can either type the state name by hand or click one of the suggestions on focus.

const capitals = FileAttachment("us-state-capitals.tsv").tsv({typed: true});
const state = view(Inputs.text({
  label: "U.S. state",
  placeholder: "Enter state name",
  datalist: capitals.map((d) => d.State)

To prevent a text input’s value from being changed, use the disabled option.

const fixed = view(Inputs.text({label: "Fixed value", value: "Can’t edit me!", disabled: true}));



The available text input options are:

If validate is not defined, text.checkValidity is used. While the input is not considered valid, changes to the input will not be reported.