Setting values
Values can change in a couple of different ways: through defaults, interacting with the UI or programmatically. The date and time picker defaults to now, 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 with the setVal method. Use the buttons option for showing/hiding set, cancel or add custom buttons.
Demo
The calendar defaults to today if not overridden. You can however manually set a default value through the
defaultSelection option.
html<label>
Default
<input id="picker" mbsc-input placeholder="Please select..." />
</label>
jsmobiscroll.datepicker('#picker', {
controls: ['date']
});
html<label>
Custom default
<input id="picker" mbsc-input placeholder="Please select..." />
</label>
jsmobiscroll.datepicker('#picker', {
controls: ['date'],
defaultSelection: new Date(2020, 11, 24)
});
While the selected value can be changed by interacting with the calendar, you can use the
setVal
method to change it programmatically.
html<button mbsc-button id="jan">Set 02-01-2020</button>
<button mbsc-button id="today">Set today</button>
jsdocument
.getElementById('today')
.addEventListener('click', function () {
instance.setVal(new Date());
}, false);
document
.getElementById('jan')
.addEventListener('click', function () {
instance.setVal(new Date(2020, 0, 2));
}, false); The button API provides shortcuts to value selection and confirmation. Add 'Now' or custom button to the component or entirely remove the 'Set' button to activate auto-set, which automatically closes the dialog if a date is selected.
mobiscroll.setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
});
mobiscroll.datepicker('#demo', {
controls: ['date'],
});
mobiscroll.datepicker('#default', {
controls: ['date'],
defaultSelection: new Date(2020, 12, 24),
});
var instance = mobiscroll.datepicker('#inline', {
controls: ['date'],
display: 'inline',
});
var nowCal = mobiscroll.datepicker('#now', {
controls: ['date'],
buttons: [
{
text: 'Now',
handler: function () {
nowCal.setVal(new Date());
nowCal.close();
},
},
'set',
'cancel',
],
});
var customCal = mobiscroll.datepicker('#custom', {
controls: ['date'],
buttons: [
{
text: '05 Jan 2020',
handler: function () {
customCal.setVal(new Date(2020, 0, 5));
customCal.close();
},
},
'set',
'cancel',
],
});
mobiscroll.datepicker('#auto', {
buttons: [
{
text: 'Close',
handler: 'cancel',
},
],
});
document.getElementById('today').addEventListener(
'click',
function () {
instance.setVal(new Date());
},
false,
);
document.getElementById('jan').addEventListener(
'click',
function () {
instance.setVal(new Date(2020, 0, 2));
},
false,
);
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<label>
Default
<input mbsc-input id="demo" placeholder="Please Select..." />
</label>
<label>
Custom default
<input mbsc-input id="default" placeholder="Please Select..." />
</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="jan">Set 02-01-2020</button>
<button mbsc-button id="today">Set today</button>
</div>
<div id="inline"></div>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Buttons API</div>
<label>
Now
<input mbsc-input id="now" placeholder="Please Select..." />
</label>
<label>
Custom
<input mbsc-input id="custom" placeholder="Please Select..." />
</label>
<label>
Auto set
<input mbsc-input id="auto" placeholder="Please Select..." />
</label>
</div>