Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 3 Datepicker - enabledHours() expects an array parameter

I am using Bootstrap 3 Datepicker on a page with 2 timepickers - when I use them together I get an error:

enabledHours() expects an array parameter

If I run just one then I don't get any error.

Here is the code I am using for the picker:

jQuery(document).ready(function () {
    $('.datetimepicker1').datetimepicker({
        showClose: true,
        format: "HHmm",
        enabledHours: [14, 15, 16, 17, 18],
        sideBySide: true
    });
});

I have created a fiddle which recreates this at https://jsfiddle.net/zgyd1qqd/1/

Additionally, I also did a fiddle with the same code (https://jsfiddle.net/zgyd1qqd/2/) but with only one picker initialized and the error is not present so, I assume, the format etc of enabledHours is actually OK?

Is there something else wrong in my code?

like image 482
bhttoan Avatar asked Jul 24 '26 14:07

bhttoan


1 Answers

It's probably because the array is referenced by both datepickers.

You could avoid this by iterating over each of the elements you are initializing the datepicker on:

Updated Example

$('.datetimepicker').each(function () {
    $(this).datetimepicker({
        showClose: true,
        format: "HHmm",
        enabledHours: [14, 15, 16, 17, 18],
        sideBySide: true
    });
});
like image 163
Josh Crozier Avatar answered Jul 27 '26 03:07

Josh Crozier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!