logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

Scroll

Drupal JS behaviors

22/02/2025, by Anonymous (not verified)
Forums
Permalink

Drupal behaviors (Drupal.behaviors) are still part of javascript in core. These behaviors will be executed on every request, including AJAX requests.

Don't forget to add a dependency to core/drupal in order to use Behaviors.

Below is an example of a Drupal Javascript Behavior.

(function ($) {
  'use strict';

  Drupal.behaviors.awesome = {
    attach: function(context, settings) {
      $('main', context).once('awesome').append('<p>Hello world</p>');
    }
  };

}(jQuery));