Home > Software design >  Get the date variable in Fullcalendar.js and Datepicker
Get the date variable in Fullcalendar.js and Datepicker

Time:01-27

I am working to add datepicker to fullcalendar so users can skip to a desired date. The issue I am having is getting the date variable from datepicker to work with calendar.gotoDate(). I am sure this is something simple, but I only know enough about javascript to struggle by.

I currently have a script using datepicker like so:

jQuery(function($) {
    $('#datepicker').datepicker({
        dateFormat: 'yy-mm-dd',
    });

    function get_datepicker() {
        var date = $('#datepicker').datepicker('getDate');
        return date;
    };
});

After I call fullcalendar (which is working flawlessly) I am trying to add a listener for the date change.

document.getElementById('datepicker').addEventListener('change', function() {
        get_datepicker();
        calendar.gotoDate(date);
    });

since fullcalendar has removed jquery, it makes it a bit tougher (for me at least) to implement other scripts like datepicker, which normally wouldn't make sense to load, but in Wordpress, it is already there...

CodePudding user response:

You are never setting the date variable inside the change listener. Either of the following should work.

document.getElementById('datepicker').addEventListener('change', function() {
    let date = get_datepicker();
    calendar.gotoDate(date);
}); 

OR

document.getElementById('datepicker').addEventListener('change', function() {
    calendar.gotoDate(get_datepicker());
});
  • Related