kez kez - 12 days ago 10
jQuery Question

jquery bootstrap datepicker Select date function

I'm trying to alert the date once user selecting the Date from Datepicker calender.

for that I tried following thing

$('#startdate').datepicker( {
onSelect: function(date) {
alert(date)
}
});


but this one not working for me

So again I tried something like this

$(function() {
$('#startdate').datetimepicker({
defaultDate: new Date()
}).on('dp.change',function(event){
alert("something")
});
});


this one also not alerting anything once selecting the date, when I debug using firebug I can see nothing wrong also.

Whats the exact way to fetch the selected date once user select the date

Answer

See the Working Demo...

I Think You Are Missing Some Jquery Library In Your Code...

Solution 1 :

$(function(){    
    $('#thedate').datepicker({
         dateFormat: 'dd-mm-yy',
         onSelect: function(date) {
            alert(date)
        },
       
    });
    
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="http://code.jquery.com/ui/1.8.21/themes/base/jquery-ui.css" rel="stylesheet"/>
Select Date : <input id="thedate" type="text" /><br />
<input id="thesubmit" type="button" value="Submit" />

Solution 2 :

$(function() {
    $( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });
    $("#datepicker").on("change",function(){
        var selected = $(this).val();
        alert(selected);
    });
});
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="datepicker"/>

Comments