Trigger Drupal.attachBehaviours when toggled

Created on 6 July 2022, over 2 years ago
Updated 27 August 2024, 5 months ago

Problem/Motivation

When Read more / Read less button is clicked, the DOM is modified to add or remove content so Drupal behaviours should be triggered.

Steps to reproduce

1. Add HTML to a field which uses the renderer provided by this module, long enough for a part of it to be initially hidden.
2. In a custom module add a Drupal behaviour in a Javascript file:

(function ($) {
  'use strict';

  Drupal.behaviors.expandCollapseFormatterTest = {
    attach: function () {
      $('a').once().click(function () {
        alert('Button was clicked !');
      });
    }
  };
})(jQuery);

3. If you placed links in the initially hidden part, the click event handler won't be added to that specific element because when behaviour is processed the link is not yet in the DOM.

Proposed resolution

Add Drupal.attachBehaviors(); in a correct place in expand_collapse_formatter.js

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain budalokko Girona

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024