how to make jQuery dollar sign work with WordPress
- This topic is empty.
-
AuthorPosts
-
EmilyGuest
I have seen this problem sometimes. The dollar sign shorthand doesn’t work and the script will simply not load. I have read to change “$” to be “jQuery” for all instance, is this really necessary? very messy…..
DebraGuestfor example, in my Child Theme files.
MarieGuestHere’s a good blog post you might want to check out:
https://dannyda.com/2022/05/22/how-to-use-jquery-where-we-cant-use-the-dollar-sign-does-not-work/
KyleGuestgreat cheers mate.
ChristopherGuestYou must use the compatibility mode in WordPress jQuery:
/* Regular jQuery */ $('.hideable').on('click', function() { $(this).hide(); }) /* Compatibility Mode */ jQuery('.hideable').on('click', function() { jQuery(this).hide(); })
This article is fantastic and tells you pretty much everything you need to know about how to correctly load jQuery scripts in WordPress. Also remember that WordPress bundles the jQuery library already, so you don’t need to embed that anywhere.
StephanieGuestTLDR here’s the common way to save keystrokes:
(function($) { $('.hideable').on('click', function() { $(this).hide(); }) })( jQuery );
It tells the script to treat any
$
sign asjQuery
string.AnnaGuesta better question is why tf did jQuery bake this into their codebase lol
AndrewGuest@Stephanie method is most common.
-
AuthorPosts