Test if element already has Bootstrap datepicker

I am working on a fairly large software platform that has multiple implementations. There is one previous implementation that uses the jQueryUI datepicker and I am now working on updating the interface using the Bootstrap datepicker.

Is there a way to check if a Boottrap upload date item is attached to a specific item, say this

<input type="text" id="user-date" class="datepicker"/>

      

Note that I am not asking if the Bootprap datepicker is loaded as in this qestion . I am rather asking for a test similar to this one for the jQueryUI datepicker to pass.

+3


source to share


1 answer


when you use bootstrap-datepicker on element it adds object to .data ("datepicker") so you can just check if it exists and check if datapick is visible you can use .data ("datepicker")). picker object like this

Html

<input type="text" id="user-date" class="datepicker"/>     

      



Js

if(!$('#user-date').data('datepicker')){// or $('#user-date').data('datepicker')==null
    console.log('datepicker is not attached');
}   
$('#user-date').datepicker();
$('#user-date').focus();//set focus to display datepicker
if($('#user-date').data('datepicker')){
    console.log('datepicker is already attached');
    if($('#user-date').data('datepicker').picker.is(":visible")){
        console.log("datepicker is visible");
    }
}

      

https://jsfiddle.net/weso8a96/

+9


source







All Articles