1
I am mounting a jQuery code with dateTimePicker Bootstrap.
I’m tried a problem which is this: when I do it in a more extensive way it works perfectly, but when I try to do it in a more simplified, more dynamic way, it doesn’t work.
How is it working:
        $("#dt_in0").datetimepicker({ format: "DD/MM/YYYY", locale: "pt-br" });
        $("#dt_fim0").datetimepicker({ format: "DD/MM/YYYY", locale: "pt-br", useCurrent: false });
        $("#dt_in0").on("dp.change", function (e) {
            $("#dt_fim0").data("DateTimePicker").minDate(e.date);
        });
        $("#dt_fim0").on("dp.change", function (e) {
            $("#dt_in0").data("DateTimePicker").maxDate(e.date);
        });
        $("#hr_in0").datetimepicker({ format: "HH:mm", locale: "pt-br" });
        $("#hr_fim0").datetimepicker({ format: "HH:mm", locale: "pt-br" });
        $("#dt_in1").datetimepicker({ format: "DD/MM/YYYY", locale: "pt-br" });
        $("#dt_fim1").datetimepicker({ format: "DD/MM/YYYY", locale: "pt-br", useCurrent: false });
        $("#dt_in1").on("dp.change", function (e) {
            $("#dt_fim1").data("DateTimePicker").minDate(e.date);
        });
        $("#dt_fim1").on("dp.change", function (e) {
            $("#dt_in1").data("DateTimePicker").maxDate(e.date);
        });
        $("#hr_in1").datetimepicker({ format: "HH:mm", locale: "pt-br" });
        $("#hr_fim1").datetimepicker({ format: "HH:mm", locale: "pt-br" });
Dynamic way and that is not working properly:
    for (x = 0; x < 2; x++) {
            var di = ("#dt_in" + x).toString();
            var df = ("#dt_fim" + x).toString();
            var hi = ("#hr_in" + x).toString();
            var hf = ("#hr_fim" + x).toString();
            $(di).datetimepicker({ format: "DD/MM/YYYY", locale: "pt-br" });
            $(df).datetimepicker({ format: "DD/MM/YYYY", locale: "pt-br", useCurrent: false });
            $(di).on("dp.change", function (e) {
                $(df).data("DateTimePicker").minDate(e.date);
            });
            $(df).on("dp.change", function (e) {
                $(di).data("DateTimePicker").maxDate(e.date);
            });
            $(hi).datetimepicker({ format: "HH:mm", locale: "pt-br" });
            $(hf).datetimepicker({ format: "HH:mm", locale: "pt-br" });
    }
The problem I’m having is this change, that is not setting the maximum and minimum date correctly.
Is there any difference in my codes that could be causing this? For I am generating the same code, in a different and more dynamic way, but the result was to be the same.
Thanks for the help.
Swap this
forfor this and see if it works.– Sam
@Sam, your code worked, but could you explain it to me? I don’t quite understand what you did
– edro
I will post an answer explaining.
– Sam