Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active November 1, 2018 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotherightthing/5023d0026ee9e6aa4db0cfae2cae5a68 to your computer and use it in GitHub Desktop.
Save dotherightthing/5023d0026ee9e6aa4db0cfae2cae5a68 to your computer and use it in GitHub Desktop.
[Using $ in $.noConflict() mode] Use jQuery's $ shortcut in No-Conflict Mode, by passing it into functions as a local variable #js #jquery

Using $ in $.noConflict() mode

How to use the dollar sign as an alias for the jQuery object

Because the jQuery object is used so often, having a short alias is very useful and the dollar sign is so conventional that when people are looking at jQuery code, they are often looking for this dollar sign as an indicator that the jQuery object is being used.

Now we could use a simple assignment to say $ = jQuery and just leave it at that, but the issue is that we don't know if the dollar sign is being used by some other JavaScript library ... So in order to make sure that the dollar sign doesn't get overridden somewhere along the lines, we need to make the dollar sign a local variable for each function that we write

Source: BuildAModule: How to use the dollar sign as an alias for the jQuery object

Using jQuery in WordPress

WordPress ships with its own version of the jQuery library ... in compatibility mode by default. That means that the typical $ shortcut for jQuery doesn't work, so it doesn't conflict with any other JavaScript libraries that use the dollar sign also, like MooTools or Prototype.

Many plugin authors and theme developers are aware of this, and they use jQuery instead of $ to be safe ... Let's stop doing that.

If the script is being loaded in the footer ... you can wrap the code in an anonymous function where you pass in jQuery to be mapped to $.

Source: Using $ instead of jQuery in WordPress

Using jQuery instead of $ in WordPress (the wrong way)

Always when trying to integrate a Mailchimp Newsletter Subscription into WordPress manually, there are a lot of problems occuring

  1. Avoid jQuery conflicts and problems

Replace the dollar signs ($) in the jQuery-Javascript code with (jQuery), especially the function mce_success_cb is responsible for a lot of problems otherwise.

Source: WordPress and Mailchimp: Custom Newsletter Subscription Form

Using $ instead of jQuery in WordPress (the right way)

// Pen deleted..
// codepen_embed height="800" theme_id="0" slug_hash="yMXVrw" default_tab="js" user="dotherightthingnz"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment