AMP
  • websites

ComboBox

Introduction

This sample demonstrastes a classic dropdown combo, with a text field which filters the dropdown elements according to the user's input. The options are rendered via amp-autocomplete.

<!doctype html>
<html >
<head>
  <title>ComboBox</title>
  <meta charset="utf-8">
  <link rel="canonical" href="https://amp.dev/documentation/examples/interactivity-dynamic-content/combobox/index.html">

  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
  <script async src="https://cdn.ampproject.org/v0.js"></script>

Setup

The amp-autocomplete component is used for suggesting according to the user's input.

<script async custom-element="amp-autocomplete" src="https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js"></script>

Autocomplete input

First we need to add an amp-autocomplete element by giving it an input field and a data source. Here we're using an inline script declaration defining a list of cities in the US as items.

For long lists of suggestions, it's possible to fetch a remote JSON endpoint using the src attribute.

We will use filter="token-prefix" as our way to match on the user input and set min-characters="0" so that the combo box will display options even when the input is empty.

<amp-autocomplete filter="token-prefix"
  min-characters="0">
  <input id="input-city"
    type="text">
  <script type="application/json">
    {
      "items": [
        "Albany, New York",
        "Annapolis, Maryland",
        "Atlanta, Georgia",
        "Augusta, Maine",
        "Austin, Texas",
        "Baton Rouge, Louisiana",
        "Bismarck, North Dakota",
        "Boise, Idaho",
        "Boston, Massachusetts",
        "Carson City, Nevada",
        "Charleston, West Virginia",
        "Cheyenne, Wyoming",
        "Columbia, South Carolina",
        "Columbus, Ohio",
        "Concord, New Hampshire",
        "Denver, Colorado",
        "Des Moines, Iowa",
        "Dover, Delaware",
        "Frankfort, Kentucky",
        "Harrisburg, Pennsylvania",
        "Hartford, Connecticut",
        "Helena, Montana",
        "Honolulu, Hawaii",
        "Indianapolis, Indiana",
        "Jackson, Mississippi",
        "Jefferson City, Missouri",
        "Juneau, Alaska",
        "Lansing, Michigan",
        "Lincoln, Nebraska",
        "Little Rock, Arkansas",
        "Madison, Wisconsin",
        "Montgomery, Alabama",
        "Montpelier, Vermont",
        "Nashville, Tennessee",
        "Oklahoma City, Oklahoma",
        "Olympia, Washington",
        "Phoenix, Arizona",
        "Pierre, South Dakota",
        "Providence, Rhode Island",
        "Raleigh, North Carolina",
        "Richmond, Virginia",
        "Sacramento, California",
        "Saint Paul, Minnesota",
        "Salem, Oregon",
        "Salt Lake City, Utah",
        "Santa Fe, New Mexico",
        "Springfield, Illinois",
        "Tallahassee, Florida",
        "Topeka, Kansas",
        "Trenton, New Jersey"
      ]
    }
  </script>
</amp-autocomplete>

Combo button

The button will focus the autocomplete input.

<button class="combo-trigger"
  on="tap:input-city.focus">
  <i class="arrow"></i>
</button>
Need further explanation?

If the explanations on this page don't cover all of your questions feel free to reach out to other AMP users to discuss your exact use case.

Go to Stack Overflow
An unexplained feature?

The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.

Edit sample on GitHub