1

I used jQuery many times ago, but always used like this: $(document). Lately i seen many times somebody using jQuery(document), I don't know difference between them, I thought they are same.

But I have very hard problem now. You know most of jQuery plugin uses $(document) method. Now I have one must use plugin that uses jQuery(document). I must include that, but after included I can't no longer use $(document) method and plugins which uses it.

How can I solve it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gereltod
  • 2,043
  • 8
  • 25
  • 39
  • 1
    They **are** the same. Maybe that plugin calls `jQuery.noConflict()` for no reason. Have a look at it's source. – Felix Kling Jul 07 '11 at 08:50
  • I found `jQuery.noConflict();` from that library. Deleted that line :), and problem **solved** :), Can you write it as "answer" for later usage those who had problems like this? So I can give "answered", and they can see this working solution. – Gereltod Jul 07 '11 at 08:54
  • What is the plugin doing? Maybe it needs it... – Felix Kling Jul 07 '11 at 09:04
  • I checked, plugin working well as before. It was 1 js file combined Jquery with http://sizzlejs.com/. – Gereltod Jul 07 '11 at 09:06
  • it would be nice if you posted some code. – T9b Jul 07 '11 at 09:08
  • @Gerelt: MMh. Ok then... but jQuery already includes sizzle... I'm confused now. But as long as it works for you ;) – Felix Kling Jul 07 '11 at 09:08

2 Answers2

1

$ is just a short reference to the global jQuery object.

window.$ === window.jQuery // true

Most plugin authors make sure that the dollar sign really is referencing the jQuery object, by putting it into a self-invoking method.

(function( $ ) {
    // $(document)
}( jQuery ));

By invoking that anonymous method with the jQuery object as argument, we can access it within the method via $.

jAndy
  • 231,737
  • 57
  • 305
  • 359
0

@jAndy provides a good explanation about $ and jQuery in general.

However for your specific problem, it seems that the plugin somehow makes $ unavailable, which either means that it overwrites $ with something else (unlikely) or that it calls jQuery.noConflict().

If it does, have a look why it is doing this. Maybe it includes another library that needs $ for its own working.

Usually, plugins should never assume that $ is available for them. @jAndy showed how to use $ if only jQuery is available.

In addition if you put all your own code in the ready handler, the first argument passed is the global jQuery object, so you can name the parameter as you want:

jQuery(function($) {
    // your code here
}); 
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143