Reputation: 1005
I am trying to add button "Add" in the inline Bootstrap Datetime picker. If we click Add button the value should display in input field closing the calndar.From Here I was trying to implement to add button.Please help me out with this. Thanks in advance.
Upvotes: 0
Views: 1065
Reputation: 451
Their is one way to do so.
Don't know which input field you were talking about because date-time is already displayed in one input field but for sake i have added a button inline to display the same data in other input field and this button also closes the popup calendar.
Below code just displays the date-time value without formatting it in other input field.
$('#datetimepicker1').datetimepicker();
$('.add').on('click', function(e) {
e.stopImmediatePropagation();
let elem = $('#datetimepicker1 input[type=text]');
let display = $('#date');
let popup = $('.bootstrap-datetimepicker-widget'); // calendar popup class
let value = $(elem).val();
$(popup).remove();
$(display).val(value).focus();
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.0.0/css/bootstrap-datetimepicker.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.0.0/js/bootstrap-datetimepicker.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class='col-sm-6'>
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<input type='text' class="form-control" />
<span class='input-group-addon'>
<span class="glyphicon glyphicon-calendar"></span>
</span>
<span class="input-group-addon add">
<span class="glyphicon glyphicon-plus add"></span>
</span>
</div>
<div class="form-group" style='margin-top: 20px;'>
<input class='form-control' type='text' id='date'>
</div>
</div>
</div>
</div>
</div>
Hope, you might get some idea from this code. :)
Upvotes: 1