Skip to main content

Value Selection

The Select component can be used to select one or multiple values from a list. The values can be of any type, for example number, string or object as described in the data binding section.

Default value and default selection​

The value and the default selection of the Select are two different things:

  • Value - the actual value of the component. It is displayed in the input, returned by the getVal method and passed to the onChange event. The initial value is taken from the element the Select is initialized on: the <option> elements with the selected attribute on a native <select>, or the value attribute on an <input>. After initialization, use the setVal method.
  • Default selection - the defaultSelection option. It is only a pre-selection inside the opened picker, shown while the component has no value. It is not set as the value: the input stays empty and nothing is returned until the user confirms the selection, for example with the Set button. Without it, the desktop UI pre-selects nothing, while the touch UI (see touchUi) pre-selects the first item in the selection highlight line.
<select id="mySelect">
<option value="mohito">Mohito</option>
<option value="longisland">Long Island</option>
<option value="margarita" selected>Margarita</option>
</select>
Initial value: shown in the input, returned as the value
const inst = $('#mySelect').mobiscroll().select({}).mobiscroll('getInst');

// Change the value after initialization
inst.setVal('longisland');
Default selection: only pre-selected in the opened picker, the input stays empty
$('#myInput').mobiscroll().select({
data: ['Mohito', 'Long Island', 'Margarita'],
defaultSelection: 'Margarita',
});

Single value selection​

By default the Select operates in single value selection mode, but can be used to select multiple values from a list as well.

Selecting a single value
$('#myInput').mobiscroll().select({
data: [
{ text: 'Mohito', value: 'mohito'},
{ text: 'Long Island', value: 'longisland'},
{ text: 'Margarita', value: 'margarita'}
],
});

Multiple value selection​

Multiselection can be enabled using the selectMultiple option.

Selecting multiple items from a list
$('#myInput').mobiscroll().select({
data: ['Free ticket', 'Free Drink', 'Free Hug'],
selectMultiple: true,
});