Reputation: 61
I have the following datepicker
settings:
startDate.datepicker({
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true,
})
endDate.datepicker({
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true,
})
As you can see, they have the same settings. How can I reuse these settings, so I can reduce the size of my code?
Upvotes: 0
Views: 58
Reputation: 117
You can define the options object as a variable and pass it into the pickers.
var datePickerOptions = {
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true,
}
startDate.datepicker(datePickerOptions)
endDate.datepicker(datePickerOptions)
Upvotes: 0
Reputation: 781706
The obvious solution is to put the options in a variable. You can also combine the jQuery objects.
startDate.add(endDate).datepicker({
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true
});
Upvotes: 0
Reputation: 6088
I would create a params
block or the like, then use the spread operator in each place I need to used the object so I could make simple additions if need be. A bit simpler, cleaner.
let params = {
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true,
};
startDate.datepicker({ ...params })
endDate.datepicker({ ...params, additionalThing: true })
Upvotes: 1
Reputation: 4947
Since you're passing an object to the datepicker()
function you can do this:
var settings = {
format: 'mm/dd/yyyy',
container: container,
todayHighlight: true,
autoclose: true
};
startDate.datepicker(settings);
endDate.datepicker(settings;
Upvotes: 0