-2

I keep getting this error "$.cookie is not a function" when trying to implement jQuery cookies.

As far as I can tell I'm implementing the plugin correctly:

First loading: /wp-content/themes/divi-child/js.cookie.js

Then loading my custom code: /wp-content/themes/divi-child/custom.js

But using the following in 'custom.js':

jQuery(document).ready(function($) {
    $.cookie("test", 1);
});

...creates this error in console:

custom.js:2 Uncaught TypeError: $.cookie is not a function at HTMLDocument. (custom.js:2)

Site can be previewed here: http://dtodd.co.uk/

Dan382
  • 976
  • 6
  • 22
  • 44
  • You are not loading a jQuery cookie library but instead [js.cookie.js](https://github.com/js-cookie/js-cookie) twice. It is using the syntax `Cookies.set('name', 'value');`- I vote to close as off topic: _This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting._ – mplungjan Apr 16 '17 at 13:07
  • Additionally it is a duplicate of the easily found http://stackoverflow.com/questions/18024539/jquery-cookie-is-not-a-function - please search for your title in google before asking next time – mplungjan Apr 16 '17 at 13:17
  • Does this answer your question? [jQuery $.cookie is not a function](https://stackoverflow.com/questions/18024539/jquery-cookie-is-not-a-function) – ElliotSchmelliot Jul 16 '20 at 22:49

2 Answers2

0

$.cookie is not a standard jQuery function and the plugin needs to be downloaded here. Make sure to include the appropriate tag where necessary. Can be downloaded from: https://github.com/carhartl/jquery-cookie

When including the cookie script, make sure to include jQuery FIRST, then the cookie plugin.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
  • Should be closed as non-repo/typo – mplungjan Apr 16 '17 at 13:10
  • @mplungjan Uhm until and unless we dont tell OP, how will he learn ? closing the question is OK, but give him some time to reply – Tushar Gupta Apr 16 '17 at 13:11
  • It is already answered in the comment. Closing does not delete the question. It is not useful for anyone but him - and I could likely have hammer closed with a duplicate – mplungjan Apr 16 '17 at 13:15
  • @mplungjan you need patience mate ! – Tushar Gupta Apr 16 '17 at 13:18
  • For what? With a minimum of research (typing the title of his question into google) he could have saved everybody time - I have loads of patience with people who have a real problem :) – mplungjan Apr 16 '17 at 13:19
0

entirely probably that WP has the new version of the cookie.js included, and downloaded from here:

https://github.com/js-cookie/js-cookie

and the answer can be seen in this post:

https://stackoverflow.com/a/38492574/1470145

pRose_la
  • 194
  • 1
  • 12