Sadikhasan
Sadikhasan

Reputation: 18598

How to select date on click from Bootstrap Datepicker

I have Bootstrap Datepicker and I want to set default date when click on Set Date button.

HTML

<div class="input-append date datepicker no-padding" data-date-format="dd-mm-yyyy">
    <input class="input-medium" id="date" size="16" type="text"><span class="add-on"><i class="icon-th"></i></span>
</div>
<a href="#" id="set_date">Set Date</a>

jQuery

$(document).ready(function() {
    $('.datepicker').datepicker();
    
    $("#set_date").click(function(){
      date ="07/01/2015";
      $("#date").val(date); 
    });
});

I set date using jQuery but it do not show as selected date. My question is How to set date on click and show as selected date.

JS Fiddle

Upvotes: 3

Views: 8020

Answers (3)

Robert
Robert

Reputation: 2824

read this http://api.jqueryui.com/datepicker/#method-setDate

here after update your example http://jsfiddle.net/tuG6C/614/

$(document).ready(function() {
    $('.datepicker').datepicker();

    $("#set_date").click(function(){
      date ="07-01-2015";
      //$("#date").val(date); 
      $( '.datepicker' ).datepicker( "setDate", date );
    });
});

Upvotes: 3

Phil
Phil

Reputation: 504

Please replace:

$("#date").val(date); 

With:

$(".datepicker").datepicker("update", date);

Upvotes: 1

Swapnil Motewar
Swapnil Motewar

Reputation: 1088

Update your javascript with

$(document).ready(function() {
    $('.datepicker').datepicker();

    $("#set_date").click(function(){
      date ="07/01/2015";

        var d = new Date();
        var curr_day = d.getDate();
        var curr_month = d.getMonth() + 1; //Months are zero based
        var curr_year = d.getFullYear();

        var today = curr_month + "/" + curr_day + "/" + curr_year;
        $("#date").val(today); 


    });
});

check js fiddle

http://jsfiddle.net/swapnilmotewar/tuG6C/609/

Upvotes: 1

Related Questions