Skip to content

jQuery noConflict()

jQuery: Avoiding Conflicts with noConflict()

Section titled “jQuery: Avoiding Conflicts with noConflict()”

How to use jQuery alongside other JavaScript libraries that might also use the $ alias.

jQuery famously uses the dollar sign ($) as a convenient shortcut for the jQuery function.

However, other JavaScript libraries or frameworks (both old and new) might also use the $ symbol for their own purposes. Common examples historically included Prototype.js, but conflicts can arise with various tools or custom scripts.

If two different scripts try to assign different meanings to the global $ variable, it creates a conflict, and usually, only the last script loaded will work correctly with $, potentially breaking the functionality of the others.

jQuery provides the $.noConflict() method to resolve this.

The jQuery.noConflict() method relinquishes jQuery’s control over the $ variable. This allows other scripts to use it.

After calling $.noConflict(), you must use the full name jQuery instead of $ to access jQuery functions.

Example: Using jQuery explicitly

// Relinquish the $ alias
$.noConflict();
// Use jQuery instead of $
jQuery(document).ready(function() {
jQuery("button").on('click', function() {
jQuery("p").text("jQuery is working correctly using its full name!");
});
});
// Or using the modern shorthand for ready():
/*
jQuery(function() {
jQuery("button").on('click', function() {
jQuery("p").text("jQuery is working correctly using its full name!");
});
});
*/

$.noConflict() returns a reference to the jQuery function. You can assign this to your own variable to create a different, safe alias.

Example: Using a custom alias jq

const jq = $.noConflict();
jq(function() { // Use jq instead of $ or jQuery
jq("button").on('click', function() {
jq("p").text("jQuery is working with the 'jq' alias!");
});
});

A common and often preferred technique is to wrap your jQuery code in an Immediately Invoked Function Expression (IIFE) or use the ready handler’s ability to pass in the jQuery object. This allows you to use $ safely inside that specific function’s scope, without conflicting with the global scope.

Example: Passing $ to the ready handler

$.noConflict();
jQuery(document).ready(function($) {
// $ safely refers to jQuery within this function
$("button").on('click', function() {
$("p").text("jQuery is working with $ inside the ready handler!");
});
});
// Modern shorthand equivalent:
/*
jQuery(function($) {
// $ safely refers to jQuery within this function
$("button").on('click', function() {
$("p").text("jQuery is working with $ inside the ready handler!");
});
});
*/

Example: Using an IIFE

$.noConflict();
(function($) {
// $ safely refers to jQuery within this IIFE
$(function() {
// DOM Ready using the safe $
$("button").on('click', function() {
$("p").text("jQuery is working with $ inside an IIFE!");
});
});
})(jQuery); // Pass jQuery object in, map it to $ locally

This last method (passing $ to the ready handler or using an IIFE) is often the cleanest way to use the concise $ syntax within your code while still avoiding global conflicts.

For a complete overview of miscellaneous jQuery methods, including noConflict(), please refer to the official jQuery API documentation.