Drupal JS behaviors

This is a quick snippet showing a basic wrapper for your JS code in Drupal. To understand it, I recommend reading the breakdown of Drupal's JS on drupal.org, which was written for Drupal 6 but mostly applies to Drupal 7, Drupal 8, and Drupal 9 too: https://drupal.org/node/304258

I also recommend reading the Mozilla Development Network's article titled "A re-introduction to JavaScript (JS Tutorial)": https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_Jav...

If you are hard core, then go one more step and read the Drupal JS coding standards: https://drupal.org/node/172169

// Drupal 7, 8, 9
(function ($) {
  'use strict';
  Drupal.behaviors.loopduplicate = {
    attach: function (context, settings) {
 
      // Custom JS goes here.
 
    }
  };
})(jQuery);
 
 
// Drupal 6
(function ($) {
  'use strict';
  Drupal.behaviors.loopduplicate = function () {
 
   // do some stuff here
 
  }
})(jQuery);

Internal References

External References

Article Type

General