Skip directly to content

Drupal 7 Quick Tip: Using jQuery's once()

on May 28th, 2011 at 6:06:36 PM

In Drupal 6, many modules as well as custom jQuery required adding a "processed" class to elements in order for those elements to avoid being processed each time an AHAH/AJAX request is made on the page. In Drupal 7, you can now use the jQuery once() plugin instead.

Whereas in the past the jQuery may have looked like this:

$('.form-title:not(.title-processed)').addClass('title-processed').click(function() {
  // functionality here
});

It's now as easy as this:

$('.form-title').once('title').click(function() {
  // functionality here
});

Using once() will automatically apply a "title-processed" class to the element, processing it only once.

Tags: 

Post new comment