Bootstrap datetimepicker: show calendar pop-up when a user clicks on input
Asked Answered
M

2

6

Is there a way to show calendar when a user clicks on input as well? Right now I have to click on the calendar icon which will then show calendar.

HTML:

<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>
</div>

JavaScript:

$(function() {
  $('#datetimepicker1').datetimepicker();
});


Example: https://jsfiddle.net/8jpmkcr5/81/

Mycenaean answered 28/7, 2017 at 11:3 Comment(1)
Possible duplicate of Bootstrap DatetimePicker Selector IssueKulturkampf
L
7

It's a little tricky, but you need to move the datetime picker to the input and manually handle the click on the input group addon. For example:

$(function() {
  $('.datetimepicker').datetimepicker();
  
  $('.datetimepicker-addon').on('click', function() {
  	$(this).prev('input.datetimepicker').data('DateTimePicker').toggle();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>


<div class='input-group date'>
  <input type='text' class="form-control datetimepicker" />
  <span class="input-group-addon datetimepicker-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>
Lupe answered 28/7, 2017 at 11:21 Comment(3)
There is no need manually handle the click on the input group addon the component has the allowInputToggle option to achieve this functionality.Kulturkampf
Well your answer now doesn't work if you click on the addon icon.Lupe
I've fixed my answer, my first comment is correct, the allowInputToggle option is enough. The problem was that I used the HTML of your snippet that has the datetimepicker class in the wrong place, that component should be initialized from the parent element to make allowInputToggle work as expected (<div class='input-group date'> in your case). Anyway, thank you for pointing out the problem with the first version of my answer, I learned better how the datetimepicker works :)Kulturkampf
K
16

You can simply use allowInputToggle option setting it to true (Default: false). As docs says:

If true, the picker will show on textbox focus and icon click when used in a button group

Here a working live sample:

$('#datetimepicker1').datetimepicker({
  allowInputToggle: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>

<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>
</div>
Kulturkampf answered 28/7, 2017 at 13:10 Comment(1)
Working perfectly. ThanksOrnelas
L
7

It's a little tricky, but you need to move the datetime picker to the input and manually handle the click on the input group addon. For example:

$(function() {
  $('.datetimepicker').datetimepicker();
  
  $('.datetimepicker-addon').on('click', function() {
  	$(this).prev('input.datetimepicker').data('DateTimePicker').toggle();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>


<div class='input-group date'>
  <input type='text' class="form-control datetimepicker" />
  <span class="input-group-addon datetimepicker-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>
Lupe answered 28/7, 2017 at 11:21 Comment(3)
There is no need manually handle the click on the input group addon the component has the allowInputToggle option to achieve this functionality.Kulturkampf
Well your answer now doesn't work if you click on the addon icon.Lupe
I've fixed my answer, my first comment is correct, the allowInputToggle option is enough. The problem was that I used the HTML of your snippet that has the datetimepicker class in the wrong place, that component should be initialized from the parent element to make allowInputToggle work as expected (<div class='input-group date'> in your case). Anyway, thank you for pointing out the problem with the first version of my answer, I learned better how the datetimepicker works :)Kulturkampf

© 2022 - 2024 — McMap. All rights reserved.