jQuery

A fun jquery trick

Working on the Forum Thread module I came across a bit of a challenge. On one user's site the call to add an inline javascript file (drupal_add_js()) was being called from nodeapi twice. Since it is calling inline javascript drupal_add_js happily puts the script on the page twice.

In this case the script in question calls a "toggle" function to hide and show the comments on a given forum node. Since the script was there twice it obediently displayed and then immediately hid the comment body. Evidently users aren't keen on this method of speed-reading.

The solution then is simple. Use an external .js file. However, in this case to make the module easy to configure I wanted to allow users the choice of which css element identifies a "comment title". (An aside that the way this works is to hide and show the siblings of the title which does impose some requirements about how the comment is formatted.) So this makes it more complex because I want to include the proper selector in the script and still include it in a file.

To accomplish this I ended up splitting the jQuery function into a couple of separate calls. The first, an inline script, does an addClass to the user-identified comment title class. Then a file-based javascript acts on the newly added class to hide and show the comments as a website user chooses on each page.

It is possible to see the results in action.

jQuery goodness

While listening to the Drupal I decided it was time to take the plunge and do a few jQuery/Javascript things here. So comments will now preview live as they are being entered and thanks to the combination of imagecache (not jQuery related) and lightbox the pictures appearing in the more recent posts now display with the lightbox effect.

Subscribe to jQuery