close

May 2024 only! Join our Discord free of charge.

Slick­Stack
Lightning-fast WordPress on Nginx

how to make jQuery dollar sign work with WordPress

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #6964
    Emily
    Guest

    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…..

    #6965
    Debra
    Guest

    for example, in my Child Theme files.

    #6966
    Marie
    Guest
    #6967
    Kyle
    Guest

    great cheers mate.

    #6968
    Christopher
    Guest

    You 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();
    })

    How To Properly Add jQuery Scripts To WordPress

    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.

    #6969
    Stephanie
    Guest

    TLDR here’s the common way to save keystrokes:

    (function($) {
    	
      $('.hideable').on('click', function() {
        $(this).hide();
      })
    	
    })( jQuery );

    It tells the script to treat any $ sign as jQuery string.

    #6970
    Anna
    Guest

    a better question is why tf did jQuery bake this into their codebase lol

    #8030
    Andrew
    Guest

    @Stephanie method is most common.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Thanks to our generous sponsors for their support!