Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Bootstrap Datepicker script has loaded

I am receiving the error:

Uncaught TypeError: undefined is not a function

When I try to set up the datepicker:

$('.datepicker').datepicker();

How can I determine if the datepicker has loaded or not?

To be clear, I'm not using jQuery UI

like image 228
Todd Ryler Avatar asked Feb 15 '15 23:02

Todd Ryler


People also ask

How can I get current date in datepicker?

On the All tab of the Property Sheet, make sure the Show Date Picker property is set to For dates. On the Data tab of the property sheet, type =Date() in the Default Value property for the field. Note: If you want to include the current time as well as the date, use the Now() function instead of Date().

How do I unbind a datepicker?

To disable a datepicker in jQuery UI we will be using disable() method which is discussed below: jQuery UI disable() method is used to disable the datepicker.

How do I initialize a datepicker?

Initialize the datepicker and specify the date format in "yyyy-mm-dd". JavaScript Code : $( "#datepicker" ). datepicker(); $( "#datepicker" ).


1 Answers

You can check to see if the datepicker script has loaded using:

if ($.fn.datepicker) {
    // ...
}

Then you can conditionally invoke the .datepicker() method if the script has loaded:

if ($.fn.datepicker) {
    $('.datepicker').datepicker();
}

Example Here

In doing so, the error won't be thrown if the script hasn't loaded.


You can also use:

if (typeof $().datepicker === 'function') {
    $('.datepicker').datepicker();
}

Example Here

like image 67
Josh Crozier Avatar answered Oct 11 '22 14:10

Josh Crozier