JavaScript Technique: Collapse Or Expand Multiple Sections Of A Web Page Simultaneously

Last reviewed/updated: 02 May 2016 | Published: 26 Apr 2014 | Status: Active
Web browser support: Internet Explorer 10+, Edge 12+, Firefox 6+, Chrome 30+, Opera 17+

1. Introduction

Collapsing/expanding multiple sections of a web page simultaneously with JavaScript is a technique for dynamically changing the displayed content of a web page. Some notes on this example:

  • The web page sections are displayed by default. To not display the web page sections by default, change the HTML source code lines 13, 19, and 25 to <div style="display:none" class="sectionSelector">.
  • The HTML element style attribute is exposed on the DOM as the JavaScript style object. The JavaScript style object gets/sets the HTML element style attribute value from/on the DOM. Specifically, assigning the JavaScript style object to a JavaScript variable gets the HTML element style attribute value from the DOM, and assigning a value to the JavaScript style object sets the HTML element style attribute value on the DOM.

1.1. Demonstration

Demonstration: JavaScript Technique: Collapse Or Expand Multiple Sections Of A Web Page Simultaneously (learnwebcoding.com).

1.2. Web Browser Support

Web browser support: IE8+, ED12+, FF3.5+, SF3.1+, CH2+, OP10+.

1.3. How To Try This Example On Your Computer

To try this example on your computer:

  1. Download the following source files to the same drive or folder on your computer:
  2. Double click the .html file.

1.4. Abbreviations

  • IE = Internet Explorer.
  • ED = Edge = EdgeHTML rendering engine (ED12+) and/or Edge browser (ED20+).
  • FF = Firefox.
  • SF = Safari.
  • CH = Chrome.
  • OP = Opera.

2. Collapse Or Expand Multiple Sections Of A Web Page Simultaneously

2.1. HTML Source Code And Notes

HTML source code: collapse_expand_all.html (learnwebcoding.com):

1     <!DOCTYPE html>
2     <html lang="en">
3      <head>
4       <title>JavaScript Technique: Collapse Or Expand Multiple Sections Of A Web Page Simultaneously</title>
5       <meta charset="UTF-8" />
6       <link rel="stylesheet" type="text/css" href="collapse_expand_all.css" />
7       <script src="collapse_expand_all.js"></script>
8      </head>
9      <body>
10      <h2>JavaScript Technique: Collapse Or Expand Multiple Sections Of A Web Page Simultaneously</h2>
11      <p><span class="interactive-object" onclick="CollapseExpandAllUtil.collapseExpandAll('expandAll')">Expand All (default)</span> | <span class="interactive-object" onclick="CollapseExpandAllUtil.collapseExpandAll('collapseAll')">Collapse All</span></p>
12      <p>Optional Section 1 Title</p>
13      <div class="sectionSelector">
14       <p>Section 1 content.<br />
15       Section 1 content.<br />
16       Section 1 content.</p>
17      </div>
18      <p>Optional Section 2 Title</p>
19      <div class="sectionSelector">
20       <p>Section 2 content.<br />
21       Section 2 content.<br />
22       Section 2 content.</p>
23      </div>
24      <p>Optional Section 3 Title</p>
25      <div class="sectionSelector">
26       <p>Section 3 content.<br />
27       Section 3 content.<br />
28       Section 3 content.</p>
29      </div>
30      <p>Web browser support: IE8+, ED12+, FF3.5+, SF3.1+, CH2+, OP10+.</p>
31     </body>
32    </html>

HTML source code notes:

  • Line 11: Creates two interactive objects that look and behave like hyperlinks, but are styled text.
    Hyperlinks can be used instead of interactive objects. However, when not navigating to a different path/URI upon the click event (as in this example), using interactive objects instead of hyperlinks avoids, among other things, unnecessarily triggering the web browser window status bar, which keeps the web browser window interface clean.
  • Line 11: The span element onclick attribute assigns/registers an event handler for the span element click event. Specifically, the value assigned to the span element onclick attribute is the event handler for the span element click event. In this example, for both interactive objects, the span element onclick attribute is assigned the CollapseExpandAllUtil.collapseExpandAll() value. Therefore, for both interactive objects, the CollapseExpandAllUtil.collapseExpandAll() method is the event handler for the span element click event.
  • Line 11: Clicking either interactive object calls the CollapseExpandAllUtil.collapseExpandAll() method and passes it a value. The value passed into the CollapseExpandAllUtil.collapseExpandAll() method indicates which interactive object was clicked/the action to perform. If the Collapse all interactive object is clicked, the "collapseAll" string value is passed. If the Expand all interactive object is clicked, the "expandAll" string value is passed.
  • Lines 13, 19, and 25: An element class attribute specifies a selector that serves as an identifier for the element. Specifically, the value assigned to the element class attribute is the element identifier. In this example, the div element class attribute is assigned the sectionSelector value. Therefore, sectionSelector is the div element identifier.

2.2. CSS Source Code And Notes

CSS source code: collapse_expand_all.css (learnwebcoding.com):

1    .interactive-object {
2      text-decoration: underline;
3    }
4    .interactive-object:hover {
5      color: #999;
6      cursor: pointer;
7    }

CSS source code notes:

  • Styles the interactive objects.
  • Not involved in dynamically changing the displayed content of the web page.

2.3. JavaScript Source Code And Notes

JavaScript source code: collapse_expand_all.js (learnwebcoding.com):

1     var CollapseExpandAllUtil = {
2      collapseExpandAll: function(actionToPerform){
3       var allSectionsReference = document.querySelectorAll(".sectionSelector");
4       var len = allSectionsReference.length;
5       for (var i = 0; i < len; i++){
6        var sectionReference = allSectionsReference[i];
7        switch (actionToPerform){
8         case "collapseAll":
9          sectionReference.style.display = "none";
10         break;
11        case "expandAll":
12         sectionReference.style.display = "block";
13         break;
14       }
15      }
16     }
17    };

JavaScript source code notes:

  • Line 1: Defines CollapseExpandAllUtil as an object literal.
  • Line 2: Defines the collapseExpandAll() function as a method of the CollapseExpandAllUtil object.
  • Line 2: Defines actionToPerform as an argument of the CollapseExpandAllUtil.collapseExpandAll() method. The value passed into the CollapseExpandAllUtil.collapseExpandAll() method is assigned to actionToPerform. actionToPerform is a local variable.
  • Line 3: Gets a reference to all elements with the class="sectionSelector" attribute, and assigns the reference (a static NodeList object) to allSectionsReference. allSectionsReference is a local variable.
  • Line 4: Gets the number of nodes in the allSectionsReference NodeList, and assigns the number to len. len is a local variable.
  • Lines 5 and 6: Iterates over each node reference in the allSectionsReference NodeList, and assigns each node reference (in this example, an HTMLDivElement object) to sectionReference. sectionReference is a local variable.
  • Lines 7 - 13: Evaluates actionToPerform and compares it to each case value. If actionToPerform is identically equal to the string value "collapseAll", execute lines 9 and 10. If actionToPerform is identically equal to the string value "expandAll", execute lines 12 and 13.
  • Line 9: Assigns display = "none" to the JavaScript sectionReference.style object, which sets style="display:none" on the DOM for each HTML element with the class="sectionSelector" attribute. This constitutes a CSS inline style and collapses/does not display the content of each HTML element with the class="sectionSelector" attribute.
  • Line 12: Assigns display = "block" to the JavaScript sectionReference.style object, which sets style="display:block" on the DOM for each HTML element with the class="sectionSelector" attribute. This constitutes a CSS inline style and expands/displays the content of each HTML element with the class="sectionSelector" attribute.
  • Lines 10 and 13: Causes the JavaScript execution to exit the switch statement. In this example, the JavaScript execution then proceeds with either another iteration of the for loop, or, upon finishing the for loop, exits the CollapseExpandAllUtil.collapseExpandAll() method.

3. Resources And Additional Information