0Day Forums
Wordpress how to use jquery and $ sign - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: CMS (https://zeroday.vip/Forum-CMS)
+---- Forum: WordPress (https://zeroday.vip/Forum-WordPress)
+---- Thread: Wordpress how to use jquery and $ sign (/Thread-Wordpress-how-to-use-jquery-and-sign)



Wordpress how to use jquery and $ sign - oberoncktknkvky - 07-24-2023

I have a simple jQuery script in a WordPress plugin that is using a jQuery wrapper like this:

$(document).ready(function(){

// jQuery code is in here

});



I am calling this script from within the WordPress Dashboard and am loading it AFTER the jQuery framework has loaded.

When I check the page in Firebug I constantly keep receiving the error message:

> TypeError: $ is not a function
>
> $(document).ready(function(){

Should I maybe wrap the script in this function:

(function($){

// jQuery code is in here

})(jQuery);

I have had this error quite a few times and am not sure how to handle it.

Any help would be greatly appreciated.


RE: Wordpress how to use jquery and $ sign - anthropogeography194 - 07-24-2023

By default when you enqueue jQuery in Wordpress you must use `jQuery`, and `$` is not used (this is for compatibility with other libraries).

Your solution of wrapping it in `function` will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).

If you must use `document.ready`, you can actually pass `$` into the function call:

jQuery(function ($) { ...


RE: Wordpress how to use jquery and $ sign - jaimehqmvtb - 07-24-2023

Try this:

<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script>
<script>
jQuery.noConflict();
(function ($) {
function readyFn() {
// Set your code here!!
}

$(document).ready(readyFn);
})(jQuery);

</script>


RE: Wordpress how to use jquery and $ sign - pecopteris759704 - 07-24-2023

You can avoid confliction like this

var jq=jQuery.noConflict();
jq(document).ready(function(){
alert("Hi this will not conflict now");
jq('selector').show();
});


RE: Wordpress how to use jquery and $ sign - laynehq - 07-24-2023

Double check your jQuery references. It is possible that you are either referencing it more than once or you are calling your function too early (before jQuery is defined). You can try as mentioned in my comments and put any jQuery reference at the top of your file (in the head) and see if that helps.

If you use the encapsulation of jQuery it shouldn't help in this case. Please try it because I think it is prettier and more obvious, but if jQuery is not defined you will get the same errors.

In the end... jQuery is not currently defined.


RE: Wordpress how to use jquery and $ sign - periosteoalveolar332444 - 07-24-2023

You have to pass $ in function()

<script>
jQuery(document).ready(function($){

// jQuery code is in here

});
</script>