how to make jQuery dollar sign work with WordPress
- This topic is empty.
-
AuthorPosts
-
Emily
GuestI 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…..
Debra
Guestfor example, in my Child Theme files.
Marie
GuestHere’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/
Kyle
Guestgreat cheers mate.
Christopher
GuestYou 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.
Stephanie
GuestTLDR 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.Anna
Guesta better question is why tf did jQuery bake this into their codebase lol
Andrew
Guest@Stephanie method is most common.
-
AuthorPosts