Drupal 8  8.0.2

Defines a behavior to be run during attach and detach phases.

Attaches all registered behaviors to a page element.

Behaviors are event-triggered actions that attach to page elements, enhancing default non-JavaScript UIs. Behaviors are registered in the Drupal.behaviors object using the method 'attach' and optionally also 'detach'.

Drupal.attachBehaviors is added below to the jQuery.ready event and therefore runs on initial page load. Developers implementing Ajax in their solutions should also call this function after new page content has been loaded, feeding in an element to be processed, in order to attach all behaviors to the new content.

Behaviors should use `var elements = .find(selector).once('behavior-name');` to ensure the behavior is attached only once to a given element. (Doing so enables the reprocessing of given elements, which may be needed on occasion despite the ability to limit behavior attachment to a particular element.)

Drupal.behaviors.behaviorName = { attach: function (context, settings) { // ... }, detach: function (context, settings, trigger) { // ... } };

{HTMLDocument|HTMLElement}[context=document] An element to attach behaviors to.
{object}[settings=drupalSettings] An object containing settings for the current context. If none is given, the global drupalSettings object is used.
See Also