Setting values
Values can change in a couple of different ways: through defaults, interacting with the UI or programmatically. The select defaults to null (nothing is selected), which can be easily overridden with the defaultSelection option. The values are set by interacting with the component and making a selection or it can be done programmatically. Use the buttons option for showing/hiding set, cancel or add custom buttons.
Demo
The select has no selected value by default. You can however manually set a default value through the
defaultSelection option. This value will show as selected when opening the control, but it needs to be submitted to be accepted as a selected value.
html<label>
Default
<input mbsc-input id="my-input" data-dropdown="true" />
</label>
<select id="my-select">
<option value="atl">Atlanta</option>
<option value="ber">Berlin</option>
<option value="bos">Boston</option>
<option value="chi">Chicago</option>
<option value="lon">London</option>
</select>
js$('#my-select').mobiscroll().select({
inputElement: document.getElementById('my-input')
});
html<label>
Custom default
<input mbsc-input id="my-input" data-dropdown="true" />
</label>
<select id="my-select">
<option value="atl">Atlanta</option>
<option value="ber">Berlin</option>
<option value="bos">Boston</option>
<option value="chi">Chicago</option>
<option value="lon">London</option>
</select>
js$('#my-select').mobiscroll().select({
inputElement: document.getElementById('my-input'),
defaultSelection: 'ber'
});
While the selected value can be changed by interacting with the select, you can use the
setVal method to change it programmatically.
html<button mbsc-button id="set-boston">Boston</button>
<button mbsc-button id="set-london">London</button>
js$('#set-boston').click(function () {
$('#my-select').mobiscroll('bos');
return false;
});
$('#set-london').click(function () {
$('#my-select').mobiscroll('lon');
return false;
}); The button API provides shortcuts to value selection and confirmation. Add a custom button and/or remove the 'Set' button to activate auto-set, which automatically closes the dialog if a value is selected.
html<label>
Auto set
<input mbsc-input id="my-input" data-dropdown="true" />
</label>
<select id="my-select">
<option value="atl">Atlanta</option>
<option value="ber">Berlin</option>
<option value="bos">Boston</option>
<option value="chi">Chicago</option>
<option value="lon">London</option>
</select>
js$('#my-select').mobiscroll().select({
inputElement: document.getElementById('my-input'),
buttons: ['cancel']
});
mobiscroll.setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
});
$(function () {
var cities = [
{
value: 'at;',
text: 'Atlanta',
},
{
value: 'ber',
text: 'Berlin',
},
{
value: 'bos',
text: 'Boston',
},
{
value: 'chi',
text: 'Chicago',
},
{
value: 'lon',
text: 'London',
},
];
$('#demo-setting-values')
.mobiscroll()
.select({
data: cities,
inputElement: document.getElementById('setting-values-input'),
});
$('#demo-setting-values-default')
.mobiscroll()
.select({
data: cities,
inputElement: document.getElementById('setting-values-default-input'),
defaultSelection: 'ber',
});
var customInst = $('#demo-setting-values-inline')
.mobiscroll()
.select({
display: 'inline',
data: cities,
})
.mobiscroll('getInst');
$('#demo-setting-values-boston').click(function () {
customInst.setVal('bos');
return false;
});
$('#demo-setting-values-london').click(function () {
customInst.setVal('lon');
return false;
});
var buttonsInst = $('#demo-setting-values-custom')
.mobiscroll()
.select({
data: cities,
buttons: [
{
text: 'Custom',
handler: function () {
buttonsInst.setTempVal('chi');
},
},
'set',
'cancel',
],
})
.mobiscroll('getInst');
$('#demo-setting-values-auto')
.mobiscroll()
.select({
data: cities,
buttons: ['cancel'],
});
});
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<label>
Default
<input mbsc-input id="demo-setting-values" placeholder="Please select..." data-dropdown="true" data-input-style="outline" data-label-style="stacked" />
</label>
<label>
Custom default
<input mbsc-input id="demo-setting-values-default" placeholder="Please select..." data-dropdown="true" data-input-style="outline" data-label-style="stacked" />
</label>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Setting a custom value</div>
<div class="mbsc-button-group-block">
<button mbsc-button id="demo-setting-values-boston">Boston</button>
<button mbsc-button id="demo-setting-values-london">London</button>
</div>
<select id="demo-setting-values-inline">
<option value="atl">Atlanta</option>
<option value="ber">Berlin</option>
<option value="chi">Chicago</option>
<option value="lon">London</option>
</select>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Buttons API</div>
<label>
Custom
<input mbsc-input id="demo-setting-values-custom" placeholder="Please select..." data-dropdown="true" data-input-style="outline" data-label-style="stacked" />
</label>
<label>
Auto set
<input mbsc-input id="demo-setting-values-auto" placeholder="Please select..." data-dropdown="true" data-input-style="outline" data-label-style="stacked" />
</label>
</div>