Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS/jQuery TypeError: jQuery(...).datepicker is not a function

I've been scratching my head on this for 2 days, pretty sure I'm just missing something simple but I can't for the life of me figure out why it's not working.

I'm trying to use the script below on my WordPress site to disable specific dates within a datepicker field in a ContactForm7 form.

I can load the script in jsfiddle with a simple input field using the same id and it works perfectly...but when I add it to my site the dates aren't disabled, and there's an error in the JS error console that says "jQuery(...).datepicker is not a function"

I've added it in my header.php file, just below the wp_head() call and just above the </head> tag. I've assigned my datepicker field with the id of dpick like the script uses.

I read that this error is commonly caused when using the $ symbol because it can conflict with other jQuery scripts within WordPress...so they suggested replacing $ with jQuery instead (which I've done in the script below)...but I'm still getting the error

var unavailableDates = ["1-9-2013", "2-9-2013", "3-9-2013", "4-9-2013", "5-9-2013"];

    function unavailable(date) {
            dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
            if (jQuery.inArray(dmy, unavailableDates) == -1) {
                return [true, ""];
            } else {
                return [false, "", "Unavailable"];
        }
    }

        jQuery(function() {
            jQuery( '#dpick' ).datepicker({
                dateFormat: 'dd MM yy',
                beforeShowDay: unavailable
        });

    });

Can't thank you enough for any help you can offer...this seems like such a simple thing but I just can't seem to wrap my head around it!

like image 295
Iconoclast Avatar asked Aug 22 '13 14:08

Iconoclast


4 Answers

I know this question is old but may be It can help other people:

The best practice to include js in wordpress is to do it using It's queuing function in the php template:

wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), 1.1, true);

(seen here)

It allows to declare the dependencies of your script, in this case, the jquery datepicker. You can check de built-in scripts that wordpress can provide in:

https://developer.wordpress.org/themes/basics/including-css-javascript/#default-scripts-included-and-registered-by-wordpress

Wordpress provide dependencies specifically for jquery datepicker so you could include your script with something like:

wp_enqueue_script( 'script', 'mypath' . '/js/script.js', array ( 'jquery', 'jquery-ui-datepicker' ), 1.7, true);

Note that if you only declare the jquery dependency you'll get an error like

'jQuery.datepicker(...) is not a function'

since the datepicker functions are not included in the base wordpres jquery.

like image 102
Miquel Correa Casablanca Avatar answered Oct 09 '22 11:10

Miquel Correa Casablanca


The are several reasons for this error:

  1. The jquery.ui is used before jquery.
  2. The $ is used by another library.
  3. The jquery lib that is referenced locally(wordpress) has different version from that using jquery.ui.
  4. When the right library and version is referenced the browser cache must be cleared.
like image 21
napoleonss Avatar answered Nov 04 '22 17:11

napoleonss


I was having the same problem. In my case I had two jQuery script references on my main page (_Layout.cshtml in ASP.NET MVC). I added 1 reference to jQuery at the top but there was 1 at the bottom of the page that I didn't notice... In Firebug this is what I saw:

enter image description here

So as you can see, jQuery UI was sitting in the middle of the conflict! :D This took me some time to figure out.

like image 6
Leniel Maccaferri Avatar answered Nov 04 '22 15:11

Leniel Maccaferri


check if all files are loaded.Should have 200 ok status.

like image 3
Pratswinz Avatar answered Nov 04 '22 17:11

Pratswinz