Posted by akanik on April 14, 2011 at 1:19am
So I have been searching around for the best way to make my jquery accordion work in my drupal site, and I think that drupal_add_library is the recommended way to go. However, I can't seem to find much information on it, or the information that I have found I cannot understand. Anyone familiar enough with drupal_add_library to give me some advice, perhaps a crash course in applying it? Alternatively, if anyone knows of a better way, or something that has worked tried and true for them, I am open to it all.
Running: Drupal 7
Thanks so much

Comments
You can just add it in the
You can just add it in the theme .info file, same way you would add css files. Pretty much the easiest way possible, and there's even some configuration options available (like if ieX, if I remember correctly).
http://drupal.org/node/171205#scripts
If you need more fine grained control, only adding it to certain pages or if certain blocks are present, and if you're really worried about page weight (mobile application?) the function could be a better route.
But how should I
I got that far (adding it to the theme file) but that only adds the jquery file, it does not call it.... right? Here is the code I'm working with.
I need to add all three of these code snippets:
$(document).ready(function() {
$("#accordion").accordion();
});
So the code I added in the .info file, but how should I then call that script with the $(document).ready script? Should I create an html (or php?) document, but that script in it, and add it to the .info file?
I'm not worried about specifying individual pages for this script to work on... it can have free reign!
Thanks so much
You can either add that in an
You can either add that in an init.js or customscript.js and then add it to your page, or toss it in a script tag in your html head (which you'll find in your html.tpl.php) like this:
<script type="text/javascript">//your code here
</script>
Check out some tutorials on how to initialize javascript in your page, I think you have all the Drupal knowledge you need to solve your problem, you just need a little more familiarity with javascript.
Use Drupal.behaviors!
If you're comfortable writing a small wrapper module, the best way to attach this would be by adding the call itself to Drupal.behaviors -- it's sort of Drupal's big $(document).ready();. It queues up all the stuff that should happen on pageload and does it for you, rather than having a whole bunch of $(document).ready()'s [or is that $(documents).ready()?]. There's a really good intro here: http://drupal.org/node/304258, and a quick google reveals this also: http://raincitystudios.com/blogs-and-pods/katherine-bailey/the-lowdown-j....
You can include the library and the script that calls it using drupal_add_js. Then if you ever change your theme, you won't lose the js functionality provided by the library.
I'm not sure about the drupal_add_library call, I haven't done much module work in D7. From here [http://drupal.org/node/756722] it looks like "This is the preferred way to deal with JavaScript and CSS which might be used by other modules.". I guess it's possible that other mods might want to use accordion...
Aces
Wonderful. I'm actually not at all comfortable with writing wrapper modules, but you have persuaded me to become comfortable with it! I got the accordion working by just adding the $(document).ready..... manually into the html.tpl.php, which I had actually tried from the start. I figured out later that I had mis-referenced my css file so it looked as if nothing was working.... silly me.
But thanks so much for everyone's help, I learn so much without even meaning to when I chat with fellow drupalers.
Best,