Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:27:15+00:00 2026-06-10T07:27:15+00:00

In drupal i have generated a list where each item is a fieldset with

  • 0

In drupal i have generated a list where each item is a fieldset with collapsible, that can contain extra information.

Because of the rather large list i want to avoid loading the extra information until a user clicks on the fieldset.

Best case scenario:

User clicks on collapsed fieldset.
Fieldset loads extra information.
Fieldset uncollapses.

I’ve copied and loaded the copy of collapse.js into my form, but I’m very new to js and jQuery, so I’m a little lost. If someone can show me how to call a function the first time the fieldset is expanded, I’m sure i can figure out the rest.

I’ve included the code from collapse.js:

(function ($) {

//Toggle the visibility of a fieldset using smooth animations.
Drupal.toggleFieldset = function (fieldset) {
  var $fieldset = $(fieldset);
   if ($fieldset.is('.collapsed')) {
    var $content = $('> .fieldset-wrapper', fieldset).hide();
    $fieldset
      .removeClass('collapsed')
      .trigger({ type: 'collapsed', value: false })
      .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Hide'));
    $content.slideDown({
      duration: 'fast',
      easing: 'linear',
      complete: function () {
        Drupal.collapseScrollIntoView(fieldset);
        fieldset.animating = false;
      },
      step: function () {
        // Scroll the fieldset into view.
        Drupal.collapseScrollIntoView(fieldset);
      } 
    });
  }
  else {
    $fieldset.trigger({ type: 'collapsed', value: true });
    $('> .fieldset-wrapper', fieldset).slideUp('fast', function () {
      $fieldset
        .addClass('collapsed')
        .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Show'));
       fieldset.animating = false;
    });
  }
};

//Scroll a given fieldset into view as much as possible.
Drupal.collapseScrollIntoView = function (node) {
  var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
  var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
  var posY = $(node).offset().top;
  var fudge = 55;
  if (posY + node.offsetHeight + fudge > h + offset) {
    if (node.offsetHeight > h) {
      window.scrollTo(0, posY);
    }
    else {
      window.scrollTo(0, posY + node.offsetHeight - h + fudge);
    }
  }
};

Drupal.behaviors.collapse = {
  attach: function (context, settings) {
    $('fieldset.collapsible', context).once('collapse', function () {
      var $fieldset = $(this);
      // Expand fieldset if there are errors inside, or if it contains an
      // element that is targeted by the uri fragment identifier. 
      var anchor = location.hash && location.hash != '#' ? ', ' + location.hash : '';
      if ($('.error' + anchor, $fieldset).length) {
        $fieldset.removeClass('collapsed');
      }

      var summary = $('<span class="summary"></span>');
      $fieldset.
        bind('summaryUpdated', function () {
          var text = $.trim($fieldset.drupalGetSummary());
           summary.html(text ? ' (' + text + ')' : '');
        })
        .trigger('summaryUpdated');

      // Turn the legend into a clickable link, but retain span.fieldset-legend
      // for CSS positioning.
      var $legend = $('> legend .fieldset-legend', this);

      $('<span class="fieldset-legend-prefix element-invisible"></span>')
        .append($fieldset.hasClass('collapsed') ? Drupal.t('Show') : Drupal.t('Hide'))
        .prependTo($legend)
        .after(' ');

      // .wrapInner() does not retain bound events.
      var $link = $('<a class="fieldset-title" href="#"></a>')
        .prepend($legend.contents())
        .appendTo($legend)
        .click(function () {
          var fieldset = $fieldset.get(0);
          // Don't animate multiple times.
          if (!fieldset.animating) {
            fieldset.animating = true;
            Drupal.toggleFieldset(fieldset);
          }
          return false;
        });

       $legend.append(summary);
    });
  }
};

})(jQuery);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:27:17+00:00Added an answer on June 10, 2026 at 7:27 am

    It looks to me like you’d have to override the whole Drupal.toggleFieldset function (just like when you are overriding a Drupal theme function.

    You could perhaps add a class to the fieldset in FormAPI then catch it in the complete function of the $content.slideDown params and fire a custom function of yours, to add a ‘loading’ graphic and make your ajax request.

    I’m assuming from your question that you are familiar enough with FormAPI/jQuery.ajax() to have a go. But let me know if not and i’ll include some snippets

    EDIT

    Here is some example code, it’d take a quite a while to setup a test environment for this, so it’just a pointer (cant create a JS fiddle for this ;))

    You might add your fieldset like this in PHP

    $form['my_fieldset'] = array(
      '#type' = 'fieldset',
      '#title' = t('My fieldset'),
      '#collapsible' = true,
      '#collapsed' = true,
      '#attributes' => array(
        'class' => array('ajax-fieldset'),
        'rel' => 'callback/url/path' // random attribute to store the link to a menu path that will return your HTML
      )
    );
    $form['my_fieldset'] = array(
      '#markup' => '<div class="response">loading...</div>'
    );
    

    You’ll also obviously have setup a menu hook returning your themed data @ callback/url/path. IMO it’s better to return JSON data and theme them in with JS templating, but the Drupal way (for the moment at least) seems to be to render HTML in the menu hook callback function.

    Then here is the JS. I’ve only included the altered complete function, rather than reproduce what you pasted. Add the complete function in to a copy of the code the re-specify the core Drupal function in your own JS file

    $content.slideDown({
      complete: function () {
        Drupal.collapseScrollIntoView(fieldset);
        fieldset.animating = false;
        if($fieldset.hasClass('ajax-fieldset')) {
          $.get(
            Drupal.settings.basePath + $fielset.attr('rel'),
            function(data) {
              $fieldset.find('.response').html(data);
            }
          )
        }
      }
    });
    

    Or, rather than messing around with the collapsible function. just create your own fieldset without the collapsible/collapsed classes and implement from scratch yourself

    ….so.. something like that 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with the Zen theme in Drupal and have discovered that I can
In Drupal, I have a table node that gets generated from mysql user data.
I have some content that is generated by the Drupal CMS that contains strings
I have some code that is generated by a Drupal view. It looks like
I am using Drupal and have sifr set on list items, and also a,
I have a Drupal form array that uses a prefix of '#' to indicate
I have a Drupal view that displays vacation leaves for a particular employee. I
I have a Drupal setup like this: Content type: Apartments Vocabulary: Areas , that
I have a Drupal 7 installation that shows the default front page. Published pages/nodes
I have two radio buttons generated by drupal. I also have a textedit box

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.