jQuery noConflict()
jQuery uses the global name $ as a shortcut. But $ is a short, attractive name, and other libraries have used it too — older ones like Prototype and MooTools, and sometimes your own code. If two scripts both define $, whichever loads last wins, and the other one breaks. $.noConflict() is jQuery's way of giving $ back.
What noConflict() does
| Call | Effect |
|---|---|
$.noConflict() |
Restores $ to whatever it was before jQuery loaded; jQuery still works |
const jq = $.noConflict() |
Same, and returns jQuery so you can use your own short name |
$.noConflict(true) |
Also restores the global jQuery name (for running two jQuery versions) |
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p id="a">Paragraph A</p>
<p id="b">Paragraph B</p>
<script>
// Another "library" that defined $ before jQuery was loaded
window.$ = function (id) { return document.getElementById(id); };
window.$.libraryName = "MiniDollar";
</script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
console.log("after loading jQuery, $ is jQuery?", $ === jQuery);
const jq = $.noConflict(); // give $ back to MiniDollar
console.log("after noConflict, $ is:", $.libraryName);
console.log("jq is jQuery?", jq === jQuery);
// Both libraries now work side by side
$("a").style.color = "crimson"; // MiniDollar API
jq("#b").css("color", "seagreen").text("Styled by jQuery (via jq)");
jQuery("#a").append(" — and jQuery still works under its full name");
</script>
</body>
</html>jQuery remembers the previous value of $ when it loads; noConflict() simply puts it back.
Keeping $ inside your own code
Typing jQuery(...) everywhere is tedious. The standard solution is to wrap your code in a function that receives jQuery as a parameter named $. Inside, $ means jQuery; outside, it means whatever the other library defined.
// 1. Immediately invoked function expression (IIFE)
(function ($) {
$(".menu").slideDown(); // jQuery here
})(jQuery);
// 2. The ready handler receives jQuery as its first argument
jQuery(function ($) {
$(".menu").slideDown(); // jQuery here too
});<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.note { display: none; padding: 8px; background: #eef5fb; border-left: 4px solid #0769ad; }
</style>
</head>
<body>
<button id="toggle">Toggle note</button>
<div class="note">This page runs with jQuery in noConflict mode.</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
jQuery.noConflict();
console.log("global $ after noConflict:", typeof window.$);
jQuery(function ($) { // $ is a local parameter = jQuery
$("#toggle").on("click", function () {
$(".note").slideToggle(200);
console.log("inside the ready handler, $.fn.jquery =", $.fn.jquery);
});
});
</script>
</body>
</html>Because nothing defined $ before jQuery here, the global $ becomes undefined after noConflict() — but the code inside the ready handler still uses $ happily.
Tip: WordPress loads jQuery in noConflict mode. That is why WordPress themes and plugins write
jQuery(function ($) { … })or(function ($) { … })(jQuery)instead of using$directly.
Two jQuery versions on one page
Occasionally a page must load two jQuery versions — for example, an old plugin that only works with jQuery 1.x. $.noConflict(true) releases both global names, so the earlier version becomes global again and you keep a private reference to the newer one:
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
const jq3 = jQuery.noConflict(true); // window.$ and window.jQuery are 1.12.4 again
console.log(jQuery.fn.jquery); // "1.12.4"
console.log(jq3.fn.jquery); // "3.7.1"
</script>This is a last resort: two copies double the download size, and plugins attach to whichever jQuery is global when they load.
Modules: no globals at all
In bundled projects you import jQuery as a module, so the conflict question disappears — $ is just a local variable in each file:
import $ from "jquery";
$(".menu").slideDown();The ExcelTAD playgrounds also support module imports, using an import map:
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
<p id="msg">Waiting for the module…</p>
<script type="module">
import $ from "jquery"; // resolved through the page's import map
$("#msg").text("jQuery " + $.fn.jquery + " imported as an ES module");
console.log("global $ defined?", typeof window.$ !== "undefined");
</script>
</body>
</html>Summary
| Situation | Solution |
|---|---|
Another library also uses $ |
jQuery.noConflict() + wrap your code in (function ($) { … })(jQuery) |
| You want a different short name | const jq = jQuery.noConflict(); |
| Two jQuery versions | const jqNew = jQuery.noConflict(true); |
| Bundler / ES modules | import $ from "jquery" — no globals |
Test yourself
After jQuery.noConflict(), which name still refers to jQuery?
$jQuery- Neither
jq, automatically
How can you keep using $ for jQuery inside your own code in noConflict mode?
- Redefine
window.$at the top of each file - Wrap the code in
(function ($) { … })(jQuery) - Call
$.conflict() - It is impossible
What does jQuery.noConflict(true) additionally restore?
- The
documentobject - The global
jQueryname - All event handlers
- The page's CSS