Select
Materialize is a modern responsive CSS framework based on Material Design by Google.
Select allows user input through specified options. Make sure you wrap it in a
.input-field
for proper alignment with other text fields.
Initialization
You must initialize the select element as shown below. In addition, you will need a separate call to init() for any dynamically generated select or any changes to an existing select.
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems, {
// specify options here
});
});
Options
Name | Type | Default | Description |
---|---|---|---|
classes | String | '' | Classes to be added to the select wrapper element. |
dropdownOptions | Object | {} | Pass options object to select dropdown initialization. |
Methods
All the methods are called on the plugin instance. You can get the plugin instance like this:
var instance = M.FormSelect.getInstance(elem);
.getSelectedValues();
Get selected values in an array.
Return Value
Array: Array of selected values.
instance.getSelectedValues();
.destroy();
Destroy plugin instance and teardown
instance.destroy();
Properties
Name | Type | Description |
---|---|---|
el | Element | The DOM element the plugin was initialized with. |
options | Object | The options the instance was initialized with. |
isMultiple | Boolean | If this is a multiple select. |
wrapper | Element | The select wrapper element. |
dropdownOptions | Element | Dropdown UL element. |
labelEl | Element | Label associated with the current select element. Is "null", if not detected. |
input | Element | Text input that shows current selected option. |
dropdown | Dropdown | Instance of the dropdown plugin for this select. |
Disabled Styles
You can also add
disabled
to the select element to make the whole thing disabled. Or if you add disabled
to
the options, the individual options will be unselectable.