Important: this documentation is not applicable to your currently selected format stories!
amp-selector
Description
Represents a control that presents a menu of options and lets the user choose from it.
Required Scripts
<script async custom-element="amp-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>
Supported Layouts
Usage
The AMP selector is a control that presents a list of options and lets the user choose one or many options; the contents of the options aren't just limited to text.
- An
amp-selectorcan contain any arbitrary HTML elements or AMP components (e.g.,amp-carousel,amp-img, etc.). - An
amp-selectorcannot contain any nestedamp-selectorcontrols. - Selectable options can be set by adding the
optionattribute to the element and assigning a value to the attribute (e.g.,<li option='value'></li>). - Disabled options can be set by adding the
disabledattribute to the element (e.g.,<li option='d' disabled></li>). - Preselected options can be set by adding the
selectedattribute to the element (e.g.,<li option='b' selected></li>). - To allow for multiple selections, add the
multipleattribute to theamp-selectorelement. By default, theamp-selectorallows for one selection at a time. - To disable the entire
amp-selector, add thedisabledattribute to theamp-selectorelement. - When an
amp-selectorcontains anameattribute and theamp-selectoris inside aformtag, if a submit event occurs on the form, theamp-selectorbehaves like a radio-button/checkbox group and submits the selected values (the ones assigned to the option) against the name of theamp-selector.
Example:
<form action="/" method="get" target="_blank" id="form1"> <amp-selector layout="container" name="single_image_select"> <ul> <li> <amp-img src="/img1.png" width="50" height="50" option="1"></amp-img> </li> <li> <amp-img src="/img2.png" width="50" height="50" option="2"></amp-img> </li> <li option="na" selected>None of the Above</li> </ul> </amp-selector> <amp-selector layout="container" name="multi_image_select" multiple> <amp-img src="/img1.png" width="50" height="50" option="1"></amp-img> <amp-img src="/img2.png" width="50" height="50" option="2"></amp-img> <amp-img src="/img3.png" width="50" height="50" option="3"></amp-img> </amp-selector> <amp-selector layout="container" name="multi_image_select_1" multiple> <amp-carousel id="carousel-1" width="200" height="60" controls> <amp-img src="/img1.png" width="80" height="60" option="a"></amp-img> <amp-img src="/img2.png" width="80" height="60" option="b" selected ></amp-img> <amp-img src="/img3.png" width="80" height="60" option="c"></amp-img> <amp-img src="/img4.png" width="80" height="60" option="d" disabled ></amp-img> </amp-carousel> </amp-selector> </form> <amp-selector layout="container" name="multi_image_select_2" multiple form="form1" > <amp-carousel id="carousel-1" width="400" height="300" type="slides" controls> <amp-img src="/img1.png" width="80" height="60" option="a"></amp-img> <amp-img src="/img2.png" width="80" height="60" option="b" selected ></amp-img> <amp-img src="/img3.png" width="80" height="60" option="c"></amp-img> <amp-img src="/img4.png" width="80" height="60" option="d"></amp-img> </amp-carousel> </amp-selector>
Clearing selections
To clear all selections when an element is tapped or clicked, set the on action attribute on the element, and specify the AMP Selector id with the clear action method.
Example:
<button on="tap:mySelector.clear">Clear Selection</button> <amp-selector id="mySelector" layout="container" multiple> <div option>Option One</div> <div option>Option Two</div> <div option>Option Three</div> </amp-selector>
Attributes
Attributes on <amp-selector>
disabled, form, multiple, name
The attributes above behave the same way as they do on a standard HTML <select> element.
keyboard-select-mode
The keyboard-select-mode attribute dictates the keyboard navigation behavior for options inside <amp-selector>.
- `none` (default): The tab key changes focus between items in the `
`. The user must press enter or space to change the selection. Arrow keys are disabled. - `focus`: Tab key gives focus to `
`. The user navigates between items with the arrow keys. Must press space or enter to change the selection. - `select`: Tab key gives focus to `
`. The selection changes as the user navigates options with arrow keys.
Attributes on <amp-selector> options
option
Indicates that the option is selectable. If a value is specified, the contents of the value is submitted with the form.
disabled, selected
The attributes above behave the same way as they do on a standard HTML <option> element.
Events
Events may trigger actions on other AMP components using the on attribute.
e.g. on="select: my-tab.show"
Read more about AMP Actions and Events.
select
amp-selector triggers the select event when the user selects an option.
Multi-selectors and single-selectors fire this when selecting or unselecting options.
Tapping disabled options does not trigger the select event.
- `event.targetOption` contains the `option` attribute value of the selected element.
- `event.selectedOptions` contains an array of the `option` attribute values of all selected elements.
Validation
See amp-selector rules in the AMP validator specification.
You've read this document a dozen times but it doesn't really cover all of your questions? Maybe other people felt the same: reach out to them on Stack Overflow.
Go to Stack Overflow Found a bug or missing a 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.
Go to GitHub