Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery no conflict

Tags:

jquery

For the most part .noConflict() is working fine for me, for example:

$jq('#no-thanks').click( function(event) {
    $jq("#olsu").fadeOut();             
});

but what is the syntax for this:

$.cookie("example", "foo", { expires: 7 });

I've tried:

$jq.cookie("example", "foo", { expires: 7 })

and

$jq().cookie("example", "foo", { expires: 7 })

any ideas?

like image 331
squeaker Avatar asked Nov 18 '11 19:11

squeaker


2 Answers

This should work:

(function($){
  // your all jQuery code inside here

  $.cookie("example", "foo", { expires: 7 });

})(jQuery);

Now you can use $ without fear of conflicting with other libraries as long as you put your jQuery code in above self-invoking anonymous function.

More Explanation Here

like image 133
Sarfraz Avatar answered Oct 01 '22 21:10

Sarfraz


You have added the jquery.cookie.js script to your page right?

jQuery.cookie is not a native jQuery function, so you need to make sure that it's being added, and that it's being correctly added to jQuery if it's happening after noConflict was called.

As for aliasing jQuery, you can use a self-executing anonymous function to alias jQuery to $ safely. Additionally the document.ready shortcut also can be used to alias jQuery to $:

(function ($) {
  //code goes here
}(jQuery));

jQuery(function ($) {
  //document.ready code goes here
});
like image 21
zzzzBov Avatar answered Oct 01 '22 21:10

zzzzBov