Search code examples
datepickerdatetimepickerbootstrap-datetimepickereonasdan-datetimepicker

Bootstrap Date picker 3 Always open


I want bootstrap datepicker 3 to remain always open. I have tried all previous solutions but I guess they are not applicable on version 3. I can show it on 'dp.hide' but it hangs my browser because of inefficiency.

Any other solutions are appreciable. I don't want calendar to be closed when I click somewhere else on browser.

Here is JavaScript code

<script type="text/javascript">
    $(function () {
        $('#datetimepicker10').datetimepicker({
            viewMode: 'years',
            format: 'MM/YYYY',
            keepOpen: true
        }).on('dp.update', function () {
            $('#datetimepicker10').datetimepicker("show");
        });

        $('#datetimepicker10').data("DateTimePicker").show();

    });
</script>

Here is html

<div class="form-group">
    <div class='input-group date' id='datetimepicker10'>
        <input type='text' class="form-control" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar">
            </span>
        </span>
    </div>
</div>

Solution

  • Instead of using input, initialize the date in a div and set inline to true.

    $("#datetimepicker").datetimepicker({
        format: 'DD/MM/YYYY HH',
        inline: true,
        sideBySide: true
    });
    function showDate()
    {
       console.log($('#datetimepicker').data("DateTimePicker").date());
    }
    .date
    {
        max-width:200px
    }
    <link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/master/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
    <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.1/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
    <script src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/master/src/js/bootstrap-datetimepicker.js"></script>
    
    <div class="date" id="datetimepicker"></div>
    <button onclick="showDate()">Get Date</button>

    If multiple datetimepickers are need to be shown open always, set $.fn.datetimepicker.defaults.inline = true; before initializing them. No need to pass inline: true in options in that case.

    Note: Be careful on setting the format. If the format doesn't include time, then time component will not be displayed. If unset, no problem.