I used drupal_add_js()
to add a jquery plugin, but there is an error TypeError: $ is undefined
at console of firebug. So how should I add the js?
I think you'd find this article in the Drupal documentation relevant and helpful.
The solution is to wrap your custom Javascript code within a closure as shown below:
(function($) {
// Your Javascript code goes here
})(jQuery);
Javascript files from all Drupal core modules follow this convention. So, you can look them up for reference.
I'm not really sure but either before/after some version of jQuery, the $
was a global alias for the jQuery object itself and could be used directly without the closure but was later removed as it conflicted with other libraries.
-
It is the problem of third part library, it used
$
without the closure. – solomon_wzs Dec 24 '12 at 7:39
jQuery('#some-id').hide();
instead of$('#some-id').hide();
and we can carry on from there. – saadlulu Dec 24 '12 at 7:09