javascript - How to grab selected dates from flatpickr - Stack Overflow

I have a range date picker at the moment. <input id="rangeDatepicker2" class="g-font-

I have a range date picker at the moment.

<input id="rangeDatepicker2" class="g-font-size-12 g-font-size-default--md" type="text" data-rp-wrapper="#rangePickerWrapper2" data-rp-type="range" data-rp-date-format="d M Y" data-rp-default-date='["01 Jan 2016", "31 Dec 2017"]'>

I would like to reuse the dates but I am unable to select them for all scenarios. I tried the code below which works great if the date selected are in the same month. But my issue is this bit of code doesn't work if the dates span multiple months.

$("#rangeDatepicker2").change(function () {
    var dates = $('.selected');
    if (dates.length == 2) {
        var start = dates[0].dateObj;
        var end = dates[1].dateObj;

        //interact with selected dates here
    }
});

May I ask how do I properly grab selected date range of a flatpickr.

I have a range date picker at the moment.

<input id="rangeDatepicker2" class="g-font-size-12 g-font-size-default--md" type="text" data-rp-wrapper="#rangePickerWrapper2" data-rp-type="range" data-rp-date-format="d M Y" data-rp-default-date='["01 Jan 2016", "31 Dec 2017"]'>

I would like to reuse the dates but I am unable to select them for all scenarios. I tried the code below which works great if the date selected are in the same month. But my issue is this bit of code doesn't work if the dates span multiple months.

$("#rangeDatepicker2").change(function () {
    var dates = $('.selected');
    if (dates.length == 2) {
        var start = dates[0].dateObj;
        var end = dates[1].dateObj;

        //interact with selected dates here
    }
});

May I ask how do I properly grab selected date range of a flatpickr.

Share Improve this question asked Nov 11, 2019 at 18:45 MasterMaster 2,1633 gold badges36 silver badges93 bronze badges 1
  • Can you add your initialization code? Where you make your "#rangeDatepicker2" a flatpickr – Jimmy Commented Nov 11, 2019 at 20:36
Add a ment  | 

1 Answer 1

Reset to default 2

Many input plugins actually pass the values in a onChange handler. The values are available there.

Solution

Pass down an onChange handler when initializing flatpickr:

$("#rangeDatepicker2").flatpickr({
    mode: 'range',
    onChange: function(dates) {
        if (dates.length == 2) {
            var start = dates[0];
            var end = dates[1];

            // interact with selected dates here
        }
    }
})

Demo

onChange documentation: https://flatpickr.js/events/#hooks

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745675469a4639661.html

相关推荐

  • javascript - How to grab selected dates from flatpickr - Stack Overflow

    I have a range date picker at the moment. <input id="rangeDatepicker2" class="g-font-

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信