Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if element already has Bootstrap datepicker

I am working on a rather big software platform that has several implementations. There is one previous implementation that uses jQueryUI datepicker, and I am now working on an interface revamp that uses Bootstrap datepicker.

Is there a way for me to test whether a Bootstrap datepicker is already attached to a certain element, let's say this one

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

Notice that I am not asking whether Bootstrap datepicker is loaded as in this qestion. I'm rather asking for a test, similar to this one for jQueryUI datepicker.

like image 998
mapto Avatar asked Apr 20 '15 16:04

mapto


1 Answers

when you use bootstrap-datepicker on a element it adds a object in .data("datepicker") so you can just check if it exists and to check if the datepicker is visible you can use the .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/

like image 146
Abraham Uribe Avatar answered Nov 17 '22 12:11

Abraham Uribe