Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="Toggle js-toggleWrapper">
    <a href="#toggleID" class="Toggle-link js-toggleAction">Toggle trigger</a>
    <div id="toggleID" class="Toggle-content js-toggleContent" aria-hidden="false">
        Toggled content
    </div><!--.Toggle-content-->
</div><!--/.Toggle-->

<div class="Toggle js-toggleWrapper">
    <a href="#toggleID" class="Toggle-link js-toggleAction">Toggle trigger</a>
    <div id="toggleID" class="Toggle-content js-toggleContent" aria-hidden="false">
        Toggled content
    </div>
</div><!--/.Toggle-->
            
          
!
            
              .Toggle {
    margin-bottom: 1em;
}
.Toggle-link {
    padding: 0.5em;
    background: lightgray;
    display: block;
    color: black;
	text-decoration: none
}
.Toggle-content {
    padding: 0.5em;
    background: lightblue;
}
/*Open and closed classes*/
.isOpen:after {
    content: " - ";
    display: inline-block;
    margin-left: 0.35em;
    width: 1em;
    height: 1em;
}
.isClosed:after {
    content: " + ";
    display: inline-block;
    margin-left: 0.35em;
    width: 1em;
    height: 1em;
}
/*JS class*/
.js-toggle {
    display: none;
}
            
          
!
            
              (function (win, doc) {
    'use strict';
    //Cutting the mustard. If the following actions are not supported, then return and do nothing
    if (!doc.querySelectorAll || !win.addEventListener || !doc.documentElement.classList) {
        return;
    }
    //Create the toggler object
    var Toggler = function(rootElement) {
        //Set a variable and store the default toggle class which hides and shows the content 
        var toggleClass = 'js-toggle';
        //Set a variable and store the selector for the toggle action
        var toggleActionSelector = '.js-toggleAction';
        //A varaible to store a class that is added to the open state of the toggle
        var openClass;
        //A varaible to store a class that is added to the closed state of the toggle
        var closedClass;
        //A variable to store all toggle links
        var toggleActions;
        //A variable to store a single toggle link
        var toggleAction;
        //A variable to store the href attribute of the toggle link
        var toggleHref;
        //A variable to store the content associagted to the link
        var target;
        //A counter to use in for loops
        var i;

        //Initialisation
        this.init = function() {
            //Get the toggle link
            toggleAction = rootElement.querySelector(toggleActionSelector);
            //Add the closed class
            toggleAction.classList.add(closedClass);
            //Get the href attribute of the toggle action link
            toggleHref = toggleAction.getAttribute('href');
            //Get the content with an id matching the href
            target = rootElement.querySelector(toggleHref);
            //Add the toggle class to that content
            target.classList.add(toggleClass);
            //Set aria-hidden attribute to true
            target.setAttribute('aria-hidden', 'true');
            //Add a click event listener to the toggle link
            toggleAction.addEventListener('click', toggle, false);   
        };

        //A method for setting a class to be used when the content is open
        this.setOpenClass = function(className) {
            openClass = className;
        };

        //A method for setting a class to be used when content is closed
        this.setClosedClass = function(className) {
            closedClass = className;
        };

        //Method to set the toggle class
        this.setToggleClass = function(className) {
            toggleClass = className;
        };
        //Toggle function
        var toggle = function(ev) {
            //Toggle the toggle class (on the toggled content)
            target.classList.toggle(toggleClass);
            //if the toggle link contains the closed class
             if (toggleAction.classList.contains(closedClass)) {
                //Remove the closed class and toggle the open class
                toggleAction.classList.remove(closedClass);
                toggleAction.classList.toggle(openClass);
                //Change the aria-hidden attribute value to false
                target.setAttribute('aria-hidden', 'false');
            //Or if the toggle link does not contain the closed class
            } else {
                //Toggle the open and closed classes
                toggleAction.classList.toggle(openClass);
                toggleAction.classList.toggle(closedClass);
                //Change the aria-hidden attribute value to true
                target.setAttribute('aria-hidden', 'true');
            }
            //Prevent the default link behaviour when clicked.
            ev.preventDefault();
        }
    };
    //Find all of the instances of the toggler pattern on the page. 
    var toggles = doc.querySelectorAll('.js-toggleWrapper');
    //Store the total number of toggler patterns
    var togglesTotal = toggles.length;
    //Counter for the for loop
    var i;
    //For storing one toggle
    var toggle;
    //Loop through each togger pattern on the page
    for ( i = 0; i < togglesTotal; i = i + 1 ) {
        //A new instance of the Toggler object is stored in the toggle variable on each iteration of the loop
        toggle = new Toggler(toggles[i]);
		//Set the open class
        toggle.setOpenClass('isOpen');
		//Set the closed class
        toggle.setClosedClass('isClosed');
        //Initialise each toggle
        toggle.init();
    }
}(this, this.document));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console