- I'm using a WordPress site.
- I'm including this script in the header.
When the script loads, I get this error:
TypeError: 'undefined' is not a function (evaluating '$(document)')
I have no idea what is causing it or what it even means.
In firebug, I get this:
$ is not a function
I have also faced such problems many time in web develoment carrier. Actually its not JS conflict, When we load html website to the browser we face no such error, but when we load these type of website through localhost we face such problem. That's because of localhost. When we load scripts through the localhost it scans the script and renders the output. But when we didn't use localhost. It just scan the output. Example, when we write php code putside the localhost and run without host we get error. But if the code is correct and is run through host we get actual output, and when we use inspect element we get the output code in HTMl format but not in PHP format this is because of rendering of the code.
This is rendering error. So to fix these jquery code error one of the solution may be using this method.
What this code does is register '$' as the varible to the function by applying jquery. Else by default the .js file is only read as javascript.
Try this snippet:
It worked for me, maybe it will help you too.
You can pass $ in function()
or you can replace
$(document);
with thisjQuery(document);
or you can use
jQuery.noConflict()
I would use this
Use jQuery's
noConflict
. It did wonders for meThat is, assuming you included jQuery on your HTML
I ran into this problem also when including jQuery in my page header, not realizing the host was already including it in the page automatically. So load your page live and check the source to see if jQuery is being linked in.