5

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?

squeaker
  • 395
  • 2
  • 7
  • 17

5 Answers5

8

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

Community
  • 1
  • 1
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
2

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
});
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

How about

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

Also you can simplyfy your life by wrapping your code in anonymous function and passing jQuery to it:

(function($){
  $('#no-thanks').click( function(event) {
    $("#olsu").fadeOut();             
    $.cookie("example", "foo", { expires: 7 });

  });
})(jQuery)
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
0

Have you tried calling jQuery directly?

jQuery.cookie("example", "foo", { expires: 7 })
Blender
  • 289,723
  • 53
  • 439
  • 496
0

I'm not sure where you got $jq, but the jQuery object is jQuery, so:

jQuery.cookie("example", "foo", {expires: 7});
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • You can store the no conflict jQuery variable into anything you wish: `$jq = jQuery.noConflict();` – Blender Nov 18 '11 at 19:27