3

I am using the Bootstrap datatimepicker (https://eonasdan.github.io/bootstrap-datetimepicker/). I'd like to disable the datetimepicker on page load.

$(document).ready(function () {
    $('#datetimepicker1').datetimepicker({
        defaultDate: new Date(),
        format: 'DD/MM/YYYY hh:mm:ss A'
    });
});

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

I have tried $('#datetimepicker1').prop('disabled', true) and $('#datetimepicker1').disable() from (http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#disable). Neither work. So what is the best way to do it?

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
  • you could just set the display: none; on your div if you would like a quick solution – Ali May 18 '17 at 20:16
  • That is not a good solution because the user can just set the display to appear and mess up the logic –  May 18 '17 at 20:23

3 Answers3

3

try the following code $('#datetimepicker1 > .form-control').prop('disabled', true); please check the link https://jsfiddle.net/komal10041992/DTcHh/32829/

komal
  • 135
  • 5
  • 1
    I am using a `datetimepicker` not a daterangerpicker –  May 18 '17 at 20:23
  • Thanks, where should this code be added? After the datetimepicker is initialized? –  May 18 '17 at 20:28
  • No need to add manually `disabled` property, the component has a [`disable()`](http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#disable) function. In your fiddle, the user can click on calendar icon and open the datetimepicker anyway. – VincenzoC May 19 '17 at 08:23
  • But still you can click calendar icon and choose date – bozydarlelutko Apr 12 '19 at 09:37
3

You were close to solution, you have to use disable(), but you have to remember that, as docs says:

Note All functions are accessed via the data attribute e.g. $('#datetimepicker').data("DateTimePicker").FUNCTION()

So you have to add .data("DateTimePicker") to your $('#datetimepicker1').disable().

Here a working sample:

$('#datetimepicker1').datetimepicker({
  defaultDate: new Date(),
  format: 'DD/MM/YYYY hh:mm:ss A'
});

//To Disable use disable() function 
$('#datetimepicker1').data("DateTimePicker").disable();
//To Enable use enable() function 
//$('#datetimepicker1').data("DateTimePicker").enable();
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>

<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>
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
VincenzoC
  • 30,117
  • 12
  • 90
  • 112
1

$(document).ready(function () {
    $('#datetimepicker1').datetimepicker({
        defaultDate: new Date(),
        format: 'DD/MM/YYYY hh:mm:ss A'
    }).find("input:first").prop("disabled", true);
});