0

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?

  • try replacing the $ with jQuery tag. Something like jQuery('#some-id').hide(); instead of $('#some-id').hide(); and we can carry on from there. – saadlulu Dec 24 '12 at 7:09
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.