Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Bootstrap Datepicker onChange issue

I am trying change link when a date is selected but whenever I select a date, I get ?day= Here's my code:

Markup:

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

It only works when I use 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

like image 488
John Guan Avatar asked Dec 16 '22 04:12

John Guan


2 Answers

According to the documentation you can get the formatted date with the format() function accessible 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();
        });
    });
like image 138
omma2289 Avatar answered Dec 27 '22 14:12

omma2289


I'm pretty 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 ;
    });
});
like image 35
ethorn10 Avatar answered Dec 27 '22 15:12

ethorn10