No Conflict Mode
As you’ve probably noticed in our code examples, jQuery uses the
$ any more so that the other library can have it.
You do that by putting jQuery into no conflict mode.
Putting jQuery into no conflict mode is easy enough, you just call the
noConflict() method like this;
Now that jQuery no longer uses the
$, you have a few options for how you’ll go about taking advantage of the jQuery library’s awesomeness without using the
First, of course, you can just use the full name,
jQuery. So a selector might look like this:
var listItems = jQuery('ul li');
You can also use a special kind of function, called a self-invoking function (big programmer words!), to contain the
$. It’s a little confusing if you try to explain it, so let’s just look at some code:
var listItems = $('ul li');
Basically, for any code you write inside that function, the
$ is equal to
jQuery, but for any code outside that function, the
$ is not equal to
Finally, you can also give jQuery a different nickname. That looks like this:
var $j = jQuery.noConflict();
var listItems = $j('ul li');
You can make that shorthand any legal variable name. And it’s fine if the variable name contains a
$, as long as there’s at least one other character there too. You can choose a short 1 or 2-character name, and save yourself a lot of typing.
See you next week for more jQuery learning!