{"id":1494,"date":"2022-08-30T15:17:00","date_gmt":"2022-08-30T15:17:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/23\/jquery-ui-datepicker-not-working-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:00","modified_gmt":"2022-08-30T15:17:00","slug":"jquery-ui-datepicker-not-working-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/jquery-ui-datepicker-not-working-collection-of-common-programming-errors\/","title":{"rendered":"jQuery UI Datepicker not working-Collection of common programming errors"},"content":{"rendered":"<p>i want to add a datepicker on a custom page but i doesn not work. WP version is 3.2.1. Those are the init string i used on wp code:<\/p>\n<pre><code>wp_enqueue_script('jquery');\nwp_enqueue_script('jquery-ui-core');\nwp_enqueue_script('jquery-ui-datepicker', get_bloginfo('template_url') . '\/js\/jquery-ui-datepicker.min.js', array('jquery','jquery-ui-core'));\nwp_enqueue_style('jquery.ui.theme', get_bloginfo('template_url') . '\/js\/smoothness\/jquery-ui-1.8.16.custom.css');\n<\/code><\/pre>\n<p>Those enqueques generate this code:<\/p>\n<pre><code>\n\n\n\n<\/code><\/pre>\n<p>On the page body, i use this code:<\/p>\n<pre><code>\n    jQuery('#datepicker').datepicker({\n        dateFormat : 'yy-mm-dd'\n    });\n\nData\n<\/code><\/pre>\n<p>The datepicker does not work. I do not know how to debug this issue. It does nothing, like the jQuery is not even there.<\/p>\n<p><strong>UPDATE:<\/strong><\/p>\n<p>With your help i managed to debug the code. I placed the script after the div and changed into:<\/p>\n<pre><code>\njQuery(document).ready(function() {\n    jQuery('#datepicker').datepicker({\n        dateFormat : 'yy-mm-dd'\n    });\n});\n\n<\/code><\/pre>\n<p>This code give me error when calling the datepicker method, the error states:<\/p>\n<pre><code>Uncaught TypeError: Object [object Object] has no method 'datepicker'\n<\/code><\/pre>\n<p>If i type jQuery(&#8216;#datepicker&#8217;) on javascript console, i get this:<\/p>\n<pre><code>[?]\n<\/code><\/pre>\n<p>I get no other errors except for that one, all the references to jQuery seems to work fine.<\/p>\n<p><strong>UPDATE 2:<\/strong><\/p>\n<p>Finally i got it working! i had to place &#8216;wp_print_scripts&#8217; instead of &#8216;init&#8217;, and i had to reposition some init code of another plugin that was conflicting. The only remaining issue are the themes&#8230; if i use the basic theme within googlecode, it works. If i use other themes (embedded in wp or linked with wp_enqueque_style) the theme will be not loaded&#8230;. if i check the generated html, there is no sign of the line that should load the jQuery theme.<\/p>\n<ol>\n<li>\n<p>I often type things wrong. So, I would start debugging you copying and pasting the links to the JS scripts in your browser and make sure they load.<\/p>\n<p>Then in Chrome go to the Wrench Menu -&gt; Tools -&gt; JavaScript Console. Here you will be able to type\/execute your JavaScript directly. I would start off my typing <code>jQuery<\/code> into the console to make sure jQuery is actually loaded. Then try doing <code>jQuery('#datepicker')<\/code> if it returns empty brackets <code>[]<\/code> then your selector is failing. If it works, try opening up the datepicker &#8211; you will probably see an error in the JS console.<\/p>\n<p>Like others have suggested, I think the problem is that the script is running before the is actually rendered. I would suggest doing the following:<\/p>\n<pre><code>\njQuery(document).ready(function() {\n    jQuery('#datepicker').datepicker({\n        dateFormat : 'yy-mm-dd'\n    });\n});\n\n<\/code><\/pre>\n<p>This will force the script to run after the entire page has loaded.<\/p>\n<p>== EXAMPLE ==<\/p>\n<pre><code>add_action( 'init', 'wp29r01_date_picker' );\nfunction wp29r01_date_picker() {\n    wp_enqueue_script( 'jquery' );\n    wp_enqueue_script( 'jquery-ui-core' );\n    wp_enqueue_script( 'jquery-datepicker', 'http:\/\/jquery-ui.googlecode.com\/svn\/trunk\/ui\/jquery.ui.datepicker.js', array('jquery', 'jquery-ui-core' ) );\n}\n\nadd_action( 'wp_footer', 'wp29r01_print_scripts');\nfunction wp29r01_print_scripts() {\n    ?&gt;\n\n    jQuery(document).ready(function() {\n        jQuery('#datepicker').datepicker();\n    })<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-23 09:51:13. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i want to add a datepicker on a custom page but i doesn not work. WP version is 3.2.1. Those are the init string i used on wp code: wp_enqueue_script(&#8216;jquery&#8217;); wp_enqueue_script(&#8216;jquery-ui-core&#8217;); wp_enqueue_script(&#8216;jquery-ui-datepicker&#8217;, get_bloginfo(&#8216;template_url&#8217;) . &#8216;\/js\/jquery-ui-datepicker.min.js&#8217;, array(&#8216;jquery&#8217;,&#8217;jquery-ui-core&#8217;)); wp_enqueue_style(&#8216;jquery.ui.theme&#8217;, get_bloginfo(&#8216;template_url&#8217;) . &#8216;\/js\/smoothness\/jquery-ui-1.8.16.custom.css&#8217;); Those enqueques generate this code: On the page body, i use this code: jQuery(&#8216;#datepicker&#8217;).datepicker({ dateFormat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1494","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1494"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1494\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}