Adding a collapsible fieldset to your nodes

Last modified: August 25, 2009 - 02:59

Editor's note: This page attempts to demonstrate how to make a collapsible form fieldset appear on a page without actually using a form. While the results are pretty, semi-functional, and even slightly cool, this method is a no-no for semantics, usability, and even breaks the basic rules of HTML. A better way to do this would be to use jQuery to toggle the collapse/expand of a styled div. When someone posts a HowTo on doing exactly that, this page will be deleted and redirected to the better, safer jQuery method.

This method has not been tested in Drupal version 5.x, but will most likely work.

The Collapsible Fieldset is a drop-down area you see underneath a titled section. For example, on the Settings page (Administer » Settings) you see the titled sections such as:

  • General settings
  • Error handling
  • Cache settings
  • etc...

When you mouse click on any of these titles, the area expands below displaying the information corresponding to the section.

I found this feature useful to incorporate on node pages that are long. Instead of using a page break, a teaser, or read more option, you can use the Expandable / Collapsible Fieldset Feature. This method also works well when you want to initially display half of a table.

Want a demonstration? Click either of the 'read more' links on this web page.

This example uses the Multiple Expandable / Collapsible Fieldsets as explained in the second section below.

To use the Collapsible Fieldset feature, do the following:

  1. Copy the following CSS code to a new file and save it as collapse.css. This code was extracted from the original drupal.css file version 4.7.5 and should work with later drupal versions. It was modified to not display the fieldset border by adding the "Removes Fieldset Border" CSS parameters shown in the first lines of code.
    collapse.css file
    /*
    * $Id: Copied from the drupal.css file,v 1.147.2.8 2006/12/14 20:20:21 killes Exp $
    *
    * Collapsing fieldsets
    */

    /* Removes Fieldset Border */
    html.js fieldset {
    border: 0; padding: 0;
    }

    html.js fieldset.collapsed {
      border-bottom-width: 0;
      border-left-width: 0;
      border-right-width: 0;
      margin-bottom: 0;
    }

    html.js fieldset.collapsed * {
      display: none;
    }

    html.js fieldset.collapsed table *,
    html.js fieldset.collapsed legend,
    html.js fieldset.collapsed legend * {
      display: inline;
    }

    html.js fieldset.collapsible legend a {
      padding-left: 15px;
      background: url(menu-expanded.png) 5px 50% no-repeat;
    }

    html.js fieldset.collapsed legend a {
      background-image: url(menu-collapsed.png);
    }

    /* Note: IE-only fix due to '* html' (breaks Konqueror otherwise). */
    * html.js fieldset.collapsible legend a {
    }
  2. Upload the collapse.css file to your site's /misc/ directory. (This is the same directory that contains the drupal.css file.) Note: the collapse.js file is a core file and is already in your /misc/ directory.
  3. Copy the following code to your node page.

    Single Expandable / Collapsible Fieldset:

    <link rel=stylesheet type="text/css" href="/misc/collapse.css">
    <script type="text/javascript" src="/misc/collapse.js"></script>

    ******* Enter the first part of your text here. *******

    <form>
    <fieldset class="collapsible collapsed">
    <legend>More Information</legend>
    <div>

    ******* Enter the last part of your text your text here. *******

    </div>
    </fieldset>
    </form>
  4. Replace the:

    ******* Enter the first part of your text here. *******

    with the first half of your text.

  5. Next, replace the:

    ******* Enter the last part of your text your text here. *******

    with the remaining last half of the text that will be initially hidden. This is the text section that is expandable and collapsible.

To use multiple Expandable / Collapsible Fieldset, use the following code:

Multiple Expandable / Collapsible Fieldsets:

<link rel=stylesheet type="text/css" href="/misc/collapse.css">
<script type="text/javascript" src="/misc/collapse.js"></script>

******* Enter the first part of your text here. *******

<!-- *** Begin collapsible 1 *** -->
<form>
<fieldset class="collapsible collapsed">
<legend>Read More &gt;&gt; Section 1</legend>
<div>

******* Enter the second part of your text your text here. *******

</div>
</fieldset>
</form>
<!-- *** End collapsible 1 *** -->

<!-- *** Begin collapsible 2 *** -->
<form>
<fieldset class="collapsible collapsed">
<legend>Read More &gt;&gt; Section 2</legend>
<div>

******* Enter the third (last) part of your text your text here. *******

</div>
</fieldset>
</form>
<!-- *** End collapsible 2 *** -->

That's it!

AttachmentSize
collapsible_fieldset_example.zip12.98 KB

A Better Way

Dustin Boston - October 15, 2007 - 04:21

Hey there's a much better way to do this. Drupal has a built in function for creating the collapsible fieldsets. You have to use the Drupal theme() function and pass in 'fieldset' as the first parameter to use it. The second parameter takes an associative array with the fieldset details. Here's an example:

$title = 'Title of your fieldset';
$body = 'What goes inside your fieldset';

    $fieldset = array(
      '#title' => $title,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#value' => $body);
    print theme('fieldset', $fieldset);

There are other parameters that you can pass in as well but you can just read the documentation yourself if you need more than this.

The really nice thing about doing it this way is that it includes the appropriate javascript (via the correct Drupal channels) so you don't have to do it manually.

If you strip out all of the core style sheets like I do, you'll need to copy the styles from the system.css to your custom style sheet because the styles aren't included, just the javascript.

Hope this helps!

Fantastic!

ashsc - November 26, 2008 - 18:31

Thanks for this. It works in D6 in case anyone's curious. This actually fixed some CSS problems I was having when I tried to accomplish the same functionality using a non-Drupal way.

A simple new way to D6

xcorex - October 2, 2008 - 13:49

<form>

<fieldset class="collapsible collapsed">
<legend>You may post code using</legend>
<div>
blahblahblah<br>
blahblahblah<br>
blahblahblah<br>
blahblahblah<br>
</div>
</fieldset>

<fieldset class="collapsible collapsed">
<legend>More information about formatting options</legend>
<div>
blahblahblah<br>
blahblahblah<br>
blahblahblah<br>
</div>
</fieldset>

</form>

This doesn't exactly work

jriedel - November 28, 2008 - 19:11

The "A simple new way to D6" seems to only work if you already have a collapsible fieldset on the page. I suspect the first call to theme('fieldset".....) makes sure the right JS script is loaded and run on the page. If you add an addtional fieldset just by using the tags, then it also works.

I think the "safest" way is to always make the theme call.

In Drupal 6 this works for

gayatri.sa - April 6, 2009 - 11:56

In Drupal 6 this works for me:

<?php
drupal_add_js
('misc/collapse.js');
?>

<fieldset class="collapsible collapsed">
  <legend class="collapse">
    <a href="#">New subscriber questions</a>
  </legend>
  <div class="fieldset-wrapper">
   This is the fieldset content....you can have anything here
  </div>
</fieldset>

1st of all THANK YOU! .. u

mark_r - July 24, 2009 - 21:19

1st of all THANK YOU! .. u stoped my rapidly raising frustration level! :)

But this code

<fieldset class="collapsible collapsed">
  <legend>New subscriber questions</legend>
   This is the fieldset content....you can have anything here
</fieldset>

will be enough. The rest does the collapse.js itself. :)

That works perfectly :) i'm

kakarot - August 25, 2009 - 14:58

That works perfectly :)

i'm using fieldsets inside a tab section based in jquery, and i don´t know why, when i switch from tab to other tab, the fieldsets stop working, even the code is exacly the same and collapse.js is loaded... any ideas?

...But this code will be enough

afterdark - October 2, 2009 - 20:27

I think it's necessary to enclose the collapsible text in a wrapper div, or you'll get the things messed up.

<fieldset class="collapsible collapsed">
  <legend>New subscriber questions</legend>
  <div class="fieldset-wrapper">This is the fieldset content....you can have anything here</div>
</fieldset>

AfterDark

Does this only work for

saepl - July 30, 2009 - 00:05

Does this only work for logged in users? For me it seems to not do anything for guest users. (I put the call to the javascript file in my page.tpl.php file)

Have you found a way to

Blackguard - August 27, 2009 - 18:10

Have you found a way to include misc/collapse.js for all pages? I don't want to give any users access to the php input filter.

Thank You

animeonx - April 24, 2009 - 01:28

In D6 this works!, But Can I add the script in the web without needing to put the script on the node?

expand all and contract all?

Abhishek Anand - August 12, 2009 - 08:57

I want to add an expand all and contract all button. How can that be done?

Try this

druplicate - September 15, 2009 - 00:52

Try this module for expand all and contract all:

http://drupal.bigbluedrop.com/api/file/contrib-jrockowitz/fieldset_helpe...

I have three fieldsets on a

fabske - September 30, 2009 - 13:36

I have three fieldsets on a node and want allow only one to be collapsed. Exactly like on this site: http://wordpress-deutschland.org/ "Wordpress auf Deutsch", "Design and Layout", ...
So, when one is getting collapsed, the other two shall close. Is it possible to do this?

 
 

Drupal is a registered trademark of Dries Buytaert.