Pasar al contenido principal

jQuery .once() un método para añadir behaviors una sola vez

Artículo completo:

jQuery .once() method for adding behaviors once

Drupal 6.x:

Drupal.behaviors.mybehavior = function(context) {
  $('input.mybehavior:not(.mybehavior-processed)', context)
    .addClass('mybehavior-processed')
    .each(function() {
      // Apply the MyBehaviour effect to the elements only once.
    });
};

Drupal 7.x:

Drupal.behaviors.mybehavior = {
  attach: function(context, settings) {
    $('input.mybehavior', context).once('mybehavior', function() {
      // Apply the MyBehaviour effect to the elements only once. 
      // For example
      $(this).click(function() {
        console.log('it works!');
      });
    });
  }
};