Why Bootstrap 4 datepicker API doesn't work?
I'm creating a calendar, somewhere in the view, I needed an input for the user to enter a date. Since I'm using bootstrap 4 and bootstrap has several addons for datepickers, I used one of them. Regarding the front-end, nothing to say, it works beautifully. When to the backend, when I use API methods, options, or events in jquery, they don't execute.
console.log($date = "data");
$('.datepicker').datepicker({
format: 'dd/mm/yyyy'
}).on('changeDate', function(e) {
$date = $('.datepicker').datepicker('getDate');
console.log($date);
});
<!-- Bootstrap CND -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- Datapicker css -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/locales/bootstrap-datepicker.pt.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<div class="input-group date" data-provide="datepicker" id="sandbox-container" style="flex: 0 0 auto;">
<input type="text" class="form-control" class="datepicker" data-date-format="dd/mm/yyyy" data-date-autoclose="true" data-date-today-highlight="true">
<div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">
<i class="fa fa-calendar" aria-hidden="true" style="align-self: stretch"></i>
</div>
</div>
</div>
</div>
</div>
[documentation] - I have been guided by the documentation, I may be using it incorrectly, can you identify the error? documentation
Can anyone help me? Greetings
[update] 2/03/2019 - options already running
According to the documentation: API Data 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 inline datepickers, use data-provide= "datepicker-inline"; these will be immediately initialized on page load, and cannot be lazily loaded.
I changed the attribute data-provide="datepicker"
to data-provide="datepicker-inline"
, with this this part of jquery
has already executed, however events are not yet dispachados
.
$('.datepicker').datepicker({
format: 'dd/mm/yyyy'
})
2 answers
Hello I did some tests and the problem is in using INPUT
you should use DIV
in datetimer, I think there is a better library than that of bootstrap you are using, try is here jQuery.{.
But anyway so it worked just try to adapt to the look you want.
$('#datepicker').datepicker();
$('#datepicker').on('changeDate', function(e){
$date = $('#datepicker').datepicker('getDate');
console.log($date);
});
<!-- Bootstrap CND -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- Datapicker css -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/locales/bootstrap-datepicker.pt.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<div class="input-group date" data-provide="datepicker" id="sandbox-container" style="flex: 0 0 auto;">
<div id="datepicker" data-date-format="dd/mm/yyyy" data-date-autoclose="true" data-date-today-highlight="true"></div>
<div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">
<i class="fa fa-calendar" aria-hidden="true" style="align-self: stretch"></i>
</div>
</div>
</div>
</div>
</div>
[Quasi-Solved]
Data API Like the bootstrap plug-ins themselves, 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 boot, and it will boot lazily, in the form of true bootstrap.
[unresolved] - in these cases I still don't know what jQuery code waits for datepicker to boot. I tried
$('.datepicker').ready(function() {
$('.datepicker').datepicker();
$('#sandbox-container input').datepicker({
format: "dd/mm/yyyy"
}).on('changeDate', function(e) {
$date = $('.datepicker').datepicker('getDate');
console.log($date);
});
});
[debbuging] - enters the function given in ready, but does not execute options or events.
[revolved] -for in-line, use data-provide = "datepicker-inline"; these will be initialized immediately on page load and cannot be loaded lazily.
In this way by changing the HTML
<div class="input-group date" data-provide="datepicker-inline" id="sandbox-container" style="flex: 0 0 auto;">
$(document).ready(function() {
$('#sandbox-container input').datepicker({
format: "dd/mm/yyyy"
}).on('changeDate', function(e) {
console.log('Aqui');
$date = $('.datepicker').datepicker('getDate');
console.log($date);
});
});