twitterfacebookgoogle plusrss feed

Pages

Tuesday, March 25, 2014

Use other libraries with jQuery using $.noConflict()


Just like jQuery, many other Javascript libraries uses $ (dollar sign) as a function. Using other Javascript library on the same page might cause error. So on the safe side, jQuery offers jQuery.noConflict() or $.noConflict; it restores all old references which are saved during jQuery initialization.

How to use it? Well, simply put jQuery.noConflict() at the top of your script. If you are experiencing "Uncaught TypeError: Property '$' of object [object Object] is not a function" error in console; its mean $ (dollar sign) is conflicting with other javascript library, put $.noConflict() on the top of the script. Still same error? no problem, in noConflict mode you cannot use shortcut of jQuery ($) use jQuery and pass $ (dollar sign) as function parameter. By including the $ in parenthesis after the function call you can then use this jQuery shortcut ($) within the code block. 

jQuery(document).ready(function($) {
   // your code here with $ sign
});

or 

jQuery(function($) {
   // your code here with $ sign
});

0 comments:

Post a Comment

comment or ask