Andrew Cargill
Andrew Cargill

Reputation: 11

Pickadate not working

I'm trying to use the Pickadate plugin. I've got a very strange problem occurring. Sometimes the plugin decides to work and other times it doesn't.

I've added some console.log()'s to picker.js picker.date.js and picker.time.js and all are firing off as expected. Also if I check $.fn.pickadate to see if it exists it does. But then when I actually call $('.datepicker').pickadate() i'm getting:

Uncaught TypeError: jQuery(...).pickadate is not a function

If I refresh the page then the majority of times it all works as expected. But the odd occasion it doesn't.

Any suggestions on what this could be?

Upvotes: 1

Views: 10191

Answers (1)

penitent_tangent
penitent_tangent

Reputation: 772

Found your question and expanding on Sharky's comment, I went from:

   $('.datepicker').pickadate({
selectMonths: true, // Creates a dropdown to control month
selectYears: 3 // Creates a dropdown of 15 years to control year
  });

to:

  $( document ).ready(function() {
       $('.datepicker').pickadate({
    selectMonths: true, // Creates a dropdown to control month
    selectYears: 3 // Creates a dropdown of 15 years to control year
  });
});

And it resolved my issue.

Edit: if it isn't clear from the code, this is an artifact of different js files loading at different speeds every time you load the page. The document.ready forces the picker initialization to wait until after all of the elements (including the picker library) are ready.

Upvotes: 6

Related Questions