Tuesday, March 31, 2015

jQuery noconflict


Many JavaScript libraries use $ as a function or variable name just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $.We know by default, jQuery uses $ as shortcut for jQuery. Thus, if you are using another JavaScript library that uses the $ variable,you can run into conflicts with jQuery.In order to avoid these conflicts, you need to put jQuery in to no-conflict mode.

$.noConflict() method to give control of the $ variable back to whichever library first implemented it. This helps to make sure that jQuery doesn't conflict with the $ object of other libraries.

Long story short, jQuery already planned for library conflicts and has a quick solution. The no-conflict mode allows you to define your own shortcut, and of course it works like a charm.
It's easy to do - just put this line in your code somewhere:
var j = jQuery.noConflict();
For Example , in your page have id attribute called "result". If you not put the jQuery noConflict means you need to use the $ jQuery to call the id attribute like $("#result"). If you assign the jQuery.noConflict to a variable , then you can call the id attribute like variablename("#result")
Example : Try this jsfiddle



Unknown Software Engineer

No comments:

Post a Comment