Using jQuery in Drupal
How to include your js file in a module or theme
Add this to the .info file
Include it in your Theme’s template.php
Use drupal_add_js in your module
Include js in yarr.module
1 2 3 4
Use drupal_add_library to add libraries included in Drupal
example add jQueryUI Accordion library
While jQuery core is included by default, jQuery UI is not, so you need to include that with drupal_add_library if you need it, as above.)
Drupal behaviors are used instead of $(document).ready();
Drupal attach behaviors
attach your functionality to the Drupal objects behavior array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Notes on the code example
- The outside wrapper is required if you wish to use the short jquery “$” syntax.
- $(‘h1 > a’) selects all of the H1 headers (and the inner anchor) that have immediate anchor children
- $(‘h1:not(:has(a))’) selects all H1 headers that do not have anchors embedded in them.
- .each, iterates over each element in the selector.
Inside the function the “this” object refers to the current DOM element. If you want the jQuery object use the $(this) syntax.