JQuery - Bootstrap Datepicker onChange issue

I try to change the link when selecting a date, but whenever I select a date, I get ?day=here my code:

Markup:

<i class="icon-calendar"></i>

It only works when using input <input type="text" class="datepicker">

JavaScript:

        $(document).ready(function(){
            $(".datepicker").datepicker({
                format: "yyyy-mm-dd",
                endDate: "-2d"
            })

            .on('changeDate', function(ev){
                var dateData = $('.datepicker').val();
                window.location.href = "?day=" + dateData ;
            });
        });

Expected results: ?day=2013-11-01

thanks

+4
source share
3 answers

According to documentationyou can get the formatted date using the function format()available through the event object:

$(document).ready(function(){
        $(".datepicker").datepicker({
            format: "yyyy-mm-dd",
            endDate: "-2d"
        })
        .on('changeDate', function(ev){
            window.location.href = "?day=" + ev.format();
        });
    });
+11
source

I am sure you can access the date via ev:

$(document).ready(function(){
    $(".datepicker").datepicker({
        format: "yyyy-mm-dd",
        endDate: "-2d"
    })

    .on('changeDate', function(ev){
        var dateData = new Date(ev.date);  // this is the change
        window.location.href = "?day=" + dateData ;
    });
});
+1
source

I have not tested it, but I think it should be more like

$(".datepicker").on('change', function(ev){
    var dateData = $(this).val();
    window.location.href = "?day=" + dateData ;
});
0
source

All Articles