Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="accordion">
  <details>
    <summary>Click here</summary>
    <p>To see the hidden content</p>
    <p><a target="_blank" href="https://gomakethings.com/how-to-build-a-progressively-enhanced-accordion-component-with-vanilla-js/">
  Testing How to build a progressively enhanced accordion component with vanilla JS
</a></p>
  </details>

  <details>
    <summary>Click again</summary>
    <p>To see more hidden content</p>
  </details>

  <details>
    <summary>Click one last time</summary>
    <p>To see more and more hidden content</p>
  </details>
</div>


              
            
!

CSS

              
                summary { 
  /* style the cursor to indicate on hover */
  cursor: pointer;
  
  /* style the summary and make it different to details content */
  color: #0069d6;
  font-weight: bold;
}
summary::-webkit-details-marker { 
  /* You can style the marker in webkit and blink (Chrome, Safari) */
    color: red;
  list-style-image: url(''); /* use alternative icon */
}


body {
   width: 80%;margin: 1em auto;
}
              
            
!

JS

              
                /**
 * Progressively Enhanced Accordion Components
 * (c) 2020 Chris Ferdinandi, MIT License, https://gomakethings.com
 */
var Accordions = (function () {
  "use strict";

  //
  // Methods
  //

  /**
   * Close any existing open accordion elements
   * @param  {Node} current The current element being opened
   * @param  {Node} parent  The wrapper for all sibling accordions
   */
  var closeOthers = function (current, parent) {
    // Get all open accordions inside parent
    var opened = Array.from(parent.querySelectorAll("details[open]"));

    // Close open ones that aren't current accordion
    opened.forEach(function (accordion) {
      if (accordion === current) return;
      accordion.removeAttribute("open");
    });
  };

  /**
   * Handle toggle event on an accordion element
   * @param  {Event} event     The Event object
   * @param  {String} selector The accordion group selector
   */
  var toggle = function (event, selector) {
    // Only run on accordions inside our selector
    var parent = event.target.closest(selector);
    if (!parent) return;

    // Only run if accordion is open
    if (!event.target.hasAttribute("open")) return;

    // Close any existing open accordions
    closeOthers(event.target, parent);
  };

  /**
   * Create the Constructor object
   */
  var Constructor = function (selector) {
    //
    // Variables
    //

    var publicAPIs = {};

    //
    // Methods
    //

    /**
     * Handle toggle events
     * @param  {Event} event The Event object
     */
    var toggleHandler = function (event) {
      toggle(event, selector);
    };

    /**
     * Destroy the current instantiation
     */
    publicAPIs.destroy = function () {
      document.removeEventListener("toggle", toggleHandler, true);
    };

    /**
     * Initialize a new instantiation
     */
    publicAPIs.init = function () {
      // Check for errors
      if (!selector || typeof selector !== "string") {
        throw new Error("Please provide a valid selector");
      }

      // Listen for when accordions open or close
      document.addEventListener("toggle", toggleHandler, true);
    };

    //
    // Initialized and return the Public APIs
    //

    publicAPIs.init();
    return publicAPIs;
  };

  //
  // Return the Constructor
  //

  return Constructor;
})();

// Instantiate plugin
var accordion = new Accordions(".accordion");

              
            
!
999px

Console