Search code examples
javascriptjquerytwitter-bootstrapjquery-uidatepicker

Datepicker : Datepicker on Modal (z-index)


I face this problem today when I try to show datepicker on Bootstrap Modal.

I'm using bootstrap-datepicker as datepicker library.

Here is my modal form :

<div class="form-group">
     <label for="message-text" class="control-label">Start Date</label>
          <div class="input-group date">
               <div class="input-group-addon">
                   <i class="fa fa-calendar"></i>
               </div>
               <input type="text" class="form-control pull-right datepicker" id="datepicker2" name="start_date">
           </div>
</div>

And here is my datepicker script :

$('#datepicker2').datepicker({
        autoclose: true,
        format: 'yyyy-mm-dd',
        zIndexOffset: 10000
    });

I already using zIndexOffset but it's not working, the calendar still shown behind the modal.

Can anyone suggest the solution ?

Thank you.


Solution

  • You should try adding the date picker container inside the modal div. You could give the modal an id like this:

    <div class="form-group" id="myModalWithDatePicker">
      <label for="start_date" class="control-label">Start Date</label>
      <div class="input-group date">
        <div class="input-group-addon">
          <i class="fa fa-calendar"></i>
        </div>
        <input type="text" class="form-control pull-right datepicker" id="datepicker2" name="start_date">
      </div>
    </div>
    

    Then you can use the container option like the following:

    $('#datepicker2').datepicker({
        autoclose: true,
        container: '#myModalWithDatePicker',
        format: 'yyyy-mm-dd'
    });