Ryan Ryan - 21 days ago 9
jQuery Question

How to get the selected date value while using Bootstrap Datepicker?

Using jquery and the Bootstrap Datepicker, how do I get the new date value I selected using the Bootstrap Datepicker?
FYI, I'm using Rails 3 and Coffescript.

I set up the datapicker using:

<input id="startdate" class="span2 datepicker" type="text" value="" name="startdate" default="2013-05-21" data-date="2013-05-21" data-behavior="datepicker">
<%= submit_tag 'Get Data using date', :id => 'get_data' %>

$(".datepicker").datepicker
endDate: new Date
format: "yyyy-mm-dd"
autoclose: true
minViewMode: 1
todayBtn: "linked"


When a user clicks on the "get data using date" button next to it, I will use jQuery to get the new date value set by the datepicker, prevent the form from submitting and run an ajax request using that date value. All the ajax is running well, except for getting the correct new date value. I tried both of the following and it doesn't give me the new date, it only return the defaults I initially set.

sd1 = $('#startdate').attr('value')
console.log sd1

sd2 = $('#startdate').attr('data-date'))
console.log sd2


I am feeling really stupid right now, but I can't find out how to get the new date value set by the bootstrap datepicker.

Answer

You can try this

$('#startdate').val()

or

$('#startdate').data('date')
Comments