jQuery noConflict()

Filed Under: jQuery

jQuery noConflict() method releases the control of the $ variable. You might have seen many JavaScript libraries which uses $ variable. In jQuery $ is used as an alias for jQuery. If you want to use the $ variable of other libraries, jQuery have to release the control over the $ variable by calling it’s noconflict method.

jQuery noConflict

Here is the general syntax for using the jQuery noConflict method:


$.noConflict()
<!-- OR --> 
jQuery.noConflict()

jQuery noConflict example

Following examples demonstrate the use of jQuery noConflict method.


<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
$.noConflict();
$(document).ready(function(){
  $("button").click(function(){
    $("div").fadeToggle("slow"); // This won’t work
  });
});
</script>
<style>
.divBlock{
width: 100px;
height: 50px;

padding: 15px;
margin: 15px;
background-color: red;
}
</style>
</head>

<body>

<div class="divBlock"></div>
<button>Test</button>

</body>
</html>

In the above example fade and toggle effect applied to <div> element.

You will not get the desired output because it uses $.noConflict() method above the $(document).ready() function. If you remove the $.noConflict() method, the above code will give you the appropriate result.

In the example below, you can see that $.noconflict() is used and the full name jQuery is used instead of $ variable in ready function and all other methods. This code will work even though the noConflict() method is used.


<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
$.noConflict();
jQuery(document).ready(function(){
  jQuery("button").click(function(){
    jQuery("div").fadeToggle("slow"); // This is working
  });
});
</script>
<style>
.divBlock{
width: 100px;
height: 50px;

padding: 15px;
margin: 15px;
background-color: red;
}
</style>
</head>

<body>

<div class="divBlock"></div>
<button>Test</button>

</body>
</html>

jQuery noConflict alias

In the below example you can see that $.noConflict() method is used with a custom alias name ‘jq‘. This code will also give you the proper output.


<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script>
jq = $.noConflict();
jq (document).ready(function(){
  jq ("button").click(function(){
    jq ("div").fadeToggle("slow"); // this is still working
  });
});
</script>
<style>
.divBlock{
width: 100px;
height: 50px;

padding: 15px;
margin: 15px;
background-color: red;
}
</style>
</head>

<body>

<div class="divBlock"></div>
<button>Test</button>

</body>
</html>

jQuery uses noConflict() method to avoid the conflict of using $ variable with other JavaScript variables. The above examples will give you an idea about the usage of noConflict method in jQuery.

Reference: API Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages