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.
If you want to get the date when the user selects it, you can do this: $("#datepicker"). datepicker({ onSelect: function() { var dateObject = $(this). datepicker('getDate'); } });
To set current date in control to which jQuery UI datepicker bind, use setDate() method. Pass date object which needs to be set as an argument to setDate() method. If you want to set it to current date then you can pass 'today' as argument.
As with bootstrap's own plugins, datepicker provides a data-api that can be used to instantiate datepickers without the need for custom javascript. For most datepickers, simply set data-provide="datepicker" on the element you want to initialize, and it will be intialized lazily, in true bootstrap fashion.
For bootstrap datepicker you can use:
$("#inputWithDatePicer").data('datepicker').getFormattedDate('yyyy-mm-dd');
You can try this
$('#startdate').val()
or
$('#startdate').data('date')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With