Reputation: 191
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!
Upvotes: 14
Views: 73781
Reputation: 347
I had a similar issue but only on firefox browser. We are using require to load the js files. I had following lines in my javascript.
require(['jquery', 'jqueryui'], function ($) {
$(document).ready(function () {
$("#form1").validationEngine({ bindButtons: $(".bindButton") });
$("#txtBidDate").datepicker({dateFormat: 'mm-dd-yy'});
$("#txtInstDate").datepicker({dateFormat: 'mm-dd-yy'});
});
Require loads js files asynchroulsy and the order is not gauranteed unless you define shim config or if your js files are all loaded as AMD. In our case, jquery was loaded after jquery-ui. We defined in main.js for require that jquery-ui has dependency on jquery. This fixed it for us
Upvotes: 0
Reputation: 81
This worked for me, for conflicting jquery codes -
<script>
$.noConflict(); //Not to conflict with other scripts
jQuery(document).ready(function($) {
$( "#datepicker" ).datepicker({
dateFormat:"yyyy-mm-dd",
changeMonth: true,
changeYear: true,
maxDate: "+0D"
});
});
</script>
Upvotes: 3
Reputation: 225
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:
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.
Upvotes: 2
Reputation: 23
I know this is an old issue. I had this same issue and it was because of including jquery.min.js along with jquery-1.10.2.js and jquery-ui.js. So by removing jquery.min.js my issue of TypeError: $(...).datepicker is not a function got resolved. Hope it helps to someone.
Upvotes: 1
Reputation: 1
I moved all scripts from footer to head then things started to work propertly
Upvotes: 0
Reputation: 1139
The are several reasons for this error:
Upvotes: 21
Reputation: 102428
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:
So as you can see, jQuery UI was sitting in the middle of the conflict! :D This took me some time to figure out.
Upvotes: 6
Reputation: 21
Okay , I have the same problem a while ago and the solution to the problem is just to add to include the jquery.ui.datepicker.js
it is included in the jquery package. However i am still wondering why I have to include this because Im using jquery.ui
before and the jquery.ui.custom
or simply the jquery js file will do the .datepicker()
function for me.
Anyway it's still nice it now working . Hope this helps.
Upvotes: 0
Reputation: 91
I don't have much experience with wordpress so I might be out of line trying to help. I have used pickadate though.
In the past I've gotten this error before
Uncaught TypeError: Object [object Object] has no method 'datepicker'
It's usually occurs because I didn't load the js files in the right order. Looking in developer tools on your site I don't see where the pickadate plugin is even loaded. I would check (if you haven't yet) to make sure the plugin is being loaded as well as being loaded in the right order.
Upvotes: 1