Wordpress: Uncaught TypeError: $ is not a function

After registering and enqueuing jQuery library into the template file, I added a JavaScript file named main.js into the page. The content of main.js looks like this:

jQuery(function () {
    $("#recipe-rating").bind("rated", function() {
        ....
    });
});

But when I reload the page and open up the console, this error displays:

main.js?ver=1.0.1:2 Uncaught TypeError: $ is not a function
    at HTMLDocument. (main.js?ver=1.0.1:2)
    at i (jquery.js?ver=1.12.4:2)
    at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
    at Function.ready (jquery.js?ver=1.12.4:2)
    at HTMLDocument.K (jquery.js?ver=1.12.4:2)

Then I checked the HTML and found that the main.js was embedded after jQuery so it should be find to use jQuery in this file.

Is there anything wrong with my code?

Asked 1 year ago ago

Write Answer: