3

I need to disabled or make readonly the icon of the yii2-date-picker-widget under the Yii2 framework.

The datepicker widget has two parts: a <span> for the icon and a <input> for the date. I can disabled the <input> using jQuery but I can't the <span>.

Datepicker

This is the jQuery code:

$("#movements-vencimiento").prop("disabled", true); // Works.
$("#vencimientoDiv").prop("disabled", true) // Not works.

This is the Yii2 code:

<div class="col col-md-4" id="vencimientoDiv">
    <?= $form->field($model, 'vencimiento')->widget(\common\widgets\DatePicker::className(), [
        'convertFormat' => true,
        'pluginOptions' => [
            'format' => 'dd/mm/yyyy',
            'autoclose' => true,
            'language' => 'es-AR',
            'todayHighlight' => 'true',
        ]
    ]) ?>
</div>

This is the HTML generated code:

<div id="vencimientoDiv">
    <div class="form-group field-movements-vencimiento">
        <label class="control-label" for="movements-vencimiento">Vencimiento</label>
        <div class="input-group date">
            <span class="input-group-addon">
                <i class="fa fa-calendar"></i>
            </span>
            <input type="text" id="movements-vencimiento" class="form-control" name="Movements[vencimiento]" disabled="">
        </div>
        <div class="help-block"></div>
    </div>
</div>
Roby Sottini
  • 2,117
  • 6
  • 48
  • 88

2 Answers2

3

A div does not have a disabled property. What you can do instead, is removing the event handlers that cause the calendar to show up, like this:

$("#vencimientoDiv *").off();

As simple as that.

Tobias Geiselmann
  • 2,139
  • 2
  • 23
  • 36
3

What you are looking for is the pointerEvents which works on all elements, you can disable and enable the click back whenever needed.

see a simple demo below

$("#clicker").on('click', function(e) {
  e.preventDefault();
  alert("clicked");
});

$("#disab").on('click', function() {
  $("#clicker").css({
    pointerEvents: 'none'
  });
})

$("#enab").on('click', function() {
  $("#clicker").css({
    pointerEvents: 'auto'
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#." id="clicker">click me</a>

<input type="button" value="Disable Click" id="disab">
<input type="button" value="enable Click" id="enab">

So in your case you will write the below to disable

$("#vencimientoDiv").css({pointerEvents:'none'});

and then enable back with

$("#vencimientoDiv").css({pointerEvents:'auto'});
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68