Home » Jquery » jquery – Uncaught TypeError: Cannot read property 'apply' of undefined – bootstrap datetimepicker

jquery – Uncaught TypeError: Cannot read property 'apply' of undefined – bootstrap datetimepicker

Posted by: admin February 22, 2020 Leave a comment

Questions:

In my application, I need to disable all end dates if the user has not selected the start date and if selected I want to disable all end dates that are less than the selected start date. According to my current code, it is not working and I am getting an error: Uncaught TypeError: Cannot read property ‘apply’ of undefined. Please, someone, check on this and help me to solve this. There are so many methods and answers to this, but nothing worked for me. Thank you.

var dateToday = new Date();  

    $('#start_date').datetimepicker({
        format: 'MM/DD/YYYY',
        minDate: dateToday,
    }).on('dp.change', function (selected) {
        if(selected.length <= 0) {
            // selected is empty
            $("#end_date").datetimepicker('disable');
        } else {
            $("#end_date").datetimepicker('enable');
        }
        $("#end_date").datetimepicker("option", "minDate", selected);
    });

    $('#end_date').datetimepicker({
        format: 'MM/DD/YYYY',
        minDate: dateToday,
    }).on('dp.change', function (selected) {
         if(selected.length <= 0) {
            // selected is empty
            $("#start_date").datetimepicker('disable');
        } else {
            $("#start_date").datetimepicker('enable');
        }
        $("#start_date").datetimepicker("option", "maxDate", selected);
    });

    //set today's date to end date as the default value
    $('#end_date').data('DateTimePicker').date(dateToday);

Error:

Uncaught TypeError: Cannot read property ‘apply’ of undefined at
HTMLDivElement. (bootstrap-datetimepicker.min.js:2)
at Function.each (VM7301 jquery.min.js:2)
at n.fn.init.each (VM7301 jquery.min.js:2)
at n.fn.init.a.fn.datetimepicker (bootstrap-datetimepicker.min.js:2)
at HTMLDivElement. (create:337)
at HTMLDivElement.dispatch (VM7301 jquery.min.js:3)
at HTMLDivElement.r.handle (VM7301 jquery.min.js:3)
at Object.trigger (VM7301 jquery.min.js:4)
at HTMLDivElement. (VM7301 jquery.min.js:4)
at Function.each (VM7301 jquery.min.js:2)

How to&Answer: