I'm trying to pass the selected date from a Bootstrap date picker to Javascript code. Plus by default, the date picker should select today's date.
Currently, when clicking the search button, the current date is shown in the alert. However, I'm having trouble actually binding it to the date picker. What am I missing/doing wrong?
Date picker code:
<form class="form form-horizontal" style="background-color:white"> <div class="form-group"> <div class="col-lg-2"> <div class="input-group addon"> <span class="input-group-addon"><i class="fa fa-calendar"></i></span> <input id="searchDate" readonly style="cursor: pointer;" data-bind="datePicker: SearchDate" class="form-control pad-bottom-when-small" type="text" data-provide="datepicker" data-date-format="dd/mm/yyyy" data-date-autoclose="true"/> </div> </div> <div class="col-lg-3"> <button class="btn btn-success" onclick="search()">Search</button> </div> </div> </form> My attempt to data bind:
<script> ko.applyBindings({ SearchDate: ko.observable(moment().toDate()), }); </script> <script type="text/javascript"> self.SearchDate = ko.observable(moment().toDate()); function search() { alert(self.SearchDate()); } </script>
ko.applyBindingsthat I've got so far.data-bind="datePicker: SearchDate"implies that there exists a binding handler in your project calleddatePicker, and you're feeding it the observable from your VM. We'd want to see it.