I use http://eonasdan.github.io/bootstrap-datetimepicker/ in my project. It is very fine.
On one page I have a form, where rows of input fields can dynamically added or removed. In each row is a "startdate" and an "enddate" input called #substitute_0_start and #substitute_0_end. The IDs are incremented with javascript when cloneNode() runs. I.e. after one clone there is one more row with #substitute_1_start and #substitute_1_end in it.
I need to adjust the settings of the "startdate" and "enddate" inputs. I use a for-loop to go over each row and adjust the settings. This works like expected, instead if I try to set minDate or maxDate of the corresponding input.
<div class="form-group col-md-3 col-xs-6">
<label for="substitute_0_start" class="control-label">ab dem</label>
<div class='input-group date substitute_start' id='substitute_0_start'>
<input type="text" class="form-control datepicker subst_start" name="substitute[0][start]" readonly="readonly"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group col-md-3 col-xs-6">
<label for="substitute_0_end" class="control-label">bis zum</label>
<div class='input-group date substitute_end' id='substitute_0_end'>
<input type="text" class="form-control datepicker subst_end" name="substitute[0][end]" readonly="readonly"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
function callDatepickerAndSelect2(){
for (a = 0; a <= i; a++){
// WORKS!
$('#substitute_'+a+'_start').datetimepicker({
format: "DD.MM.YYYY",
minDate: moment().add(1, 'minutes')
});
$('#substitute_'+a+'_end').datetimepicker({
format: "DD.MM.YYYY",
minDate: moment().add(1, 'days')
});
// WORKS NOT FOR CLONED ITEMS
$("#interval_start").on("dp.change", function (e) {
$('.substitute_start').data("DateTimePicker").minDate(e.date);
$('.substitute_end').data("DateTimePicker").minDate(e.date);
});
$("#interval_end").on("dp.change", function (e) {
$('.substitute_start').data("DateTimePicker").maxDate(e.date);
$('.substitute_end').data("DateTimePicker").maxDate(e.date);
});
// THIS DOES NOT WORK!
$("#substitute_"+a+"_start").on("dp.change", function (e) {
$("#substitute_"+ a +"_end").data("DateTimePicker").minDate(e.date);
});
$("#substitute_"+a+"_end").on("dp.change", function (e) {
$("#substitute_"+ a +"_start").data("DateTimePicker").maxDate(e.date);
});
};
}
Everytime on of the two last dp.change() are fired, I get an error Cannot read property 'minDate' of undefined or Cannot read property 'maxDate' of undefined .
I can't find a solution how to set maxDate of #substitute_X_end if #substitute_X_start is changed (and of curse the other direction).
Does anyone have an idea about set maxDate/minDate to the corresponding inputs initially and if I did a cloneNode()??
EDIT Also I've investigated, that the middle part does not work for cloned elements. For me it feels like cloned elements are ownly viewable, but I can't access them with javascript. I think I need another solution for cloning the whole DIV :-(
EDIT 2 I setup a fiddle. Checkout here https://jsfiddle.net/qs8kf6wj/ (sorry, the datetimepicker does not work there, wasn't able to find out why)
A bit better, without trouble minDate and maxDate AND some more improvements!