css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <h1>Simple Accessible Accordion</h1>
<h2>Faq Section 1</h2>
<dl class="faq js-faq">
  <dt class="faq__question js-faq-question">Question 1</dt>
  <dd class="faq__answer js-faq-answer">
    <p>Nam non nulla sed est ultricies dapibus. Pellentesque nulla erat, <a href="#">pharetra non lorem</a> ac, tempor viverra urna. Donec non leo fermentum, egestas diam a, mollis mauris.</p>
  </dd>
  <dt class="faq__question js-faq-question">Question 2</dt>
  <dd class="faq__answer js-faq-answer">
    <p>Nam non nulla sed est ultricies dapibus. Pellentesque nulla erat, pharetra non lorem ac, tempor viverra urna. Donec non leo fermentum, egestas diam a, mollis mauris.</p>
  </dd>
  <dt class="faq__question js-faq-question">Question 3</dt>
  <dd class="faq__answer js-faq-answer">
    <p>Nam non nulla sed est ultricies dapibus. Pellentesque nulla erat, pharetra non lorem ac, tempor viverra <a href="#">urna</a>. Donec non leo fermentum, egestas diam a, mollis mauris.</p>
  </dd>
</dl>
<h2>Faq Section 2</h2>
<dl class="faq js-faq">
  <dt class="faq__question js-faq-question">Question 4</dt>
  <dd class="faq__answer js-faq-answer">
    <p>Nam non nulla sed est ultricies dapibus. Pellentesque nulla erat, pharetra non lorem ac, tempor viverra urna. Donec non leo fermentum, egestas diam a, mollis mauris.</p>
  </dd>
  <dt class="faq__question js-faq-question">Question 5</dt>
  <dd class="faq__answer js-faq-answer">
    <p>Nam non nulla <a href="#">sed est ultricies</a> dapibus. Pellentesque nulla erat, pharetra non lorem ac, tempor viverra urna. Donec <a href="#">non leo</a> fermentum, egestas diam a, mollis mauris.</p>
  </dd>
  <dt class="faq__question js-faq-question">Question 6</dt>
  <dd class="faq__answer js-faq-answer">
    <p>Nam non nulla sed est ultricies dapibus. Pellentesque nulla erat, pharetra non lorem ac, tempor viverra urna. Donec non leo fermentum, egestas diam a, mollis mauris.</p>
  </dd>
</dl>
            
          
!
            
              .faq__question {
  color: Crimson;
  cursor: pointer;
  padding: .5em 1em;
}

.faq__answer {
  display: none;
}

.faq__answer.active {
  display: block;
}

.no-js .faq__question {
  color: black;
  cursor: default;
}

.no-js .faq__answer {
  display: block;
}

            
          
!
            
                    (function (document, window, undefined) {

  'use strict';

  // Vars
  var faqSection = $('.js-faq'),
    faqQuestion = $('.js-faq-question'),
    faqAnswer = $('.js-faq-answer'),
    showOneAnswerAtATime = false;

  /**
   * Save question focus
   */
  var saveFocus = function (elem, thisSectionFaqQuestions) {

    // Reset other tab attributes
    thisSectionFaqQuestions.each(function () {
      $(this).attr('tabindex', '-1');
      $(this).attr('aria-selected', 'false');
    });

    // Set this tab attributes
    elem.attr({
      'tabindex': '0',
      'aria-selected': 'true'
    });

  };

  /**
   * Show answer on click
   */
  var showAnswer = function (elem, thisSectionFaqQuestions) {
    var thisFaqAnswer = elem.next();
    
    // Save focus
    saveFocus(elem, thisSectionFaqQuestions);

    // Set this tab attributes
    if (thisFaqAnswer.hasClass('active')) {
      // Hide answer
      thisFaqAnswer.removeClass('active');      
      elem.attr('aria-expanded', 'false');      
      thisFaqAnswer.attr('aria-hidden', 'true');
    } else {
      if (showOneAnswerAtATime) {
        // Hide all answers
        faqAnswer.removeClass('active').attr('aria-hidden', 'true');
        faqQuestion.attr('aria-expanded', 'false');
      }
      
      // Show answer
      thisFaqAnswer.addClass('active');      
      elem.attr('aria-expanded', 'true');      
      thisFaqAnswer.attr('aria-hidden', 'false');
    }
  };

  /**
   * Keyboard interaction
   */
  var keyboardInteraction = function (elem, e, thisSectionFaqQuestions) {
    var keyCode = e.which,
      nextQuestion = elem.next().next().is('dt') ? elem.next().next() : false,
      previousQuestion = elem.prev().prev().is('dt') ? elem.prev().prev() : false,
      firstQuestion = elem.parent().find('dt:first'),
      lastQuestion = elem.parent().find('dt:last');

    switch(keyCode) {
    // Left/Up
    case 37:
    case 38:
      e.preventDefault();
      e.stopPropagation();

      // Check for previous question
      if (!previousQuestion) {
        // No previous, set focus on last question
        lastQuestion.focus();
      } else {
        // Move focus to previous question
        previousQuestion.focus();
      }

      break;

    // Right/Down
    case 39:
    case 40:
      e.preventDefault();
      e.stopPropagation();

      // Check for next question
      if (!nextQuestion) {
        // No next, set focus on first question
        firstQuestion.focus();
      } else {
        // Move focus to next question
        nextQuestion.focus();
      }

      break;

    // Home
    case 36:
      e.preventDefault();
      e.stopPropagation();

      // Set focus on first question
      firstQuestion.focus();
      break;

    // End
    case 35:
      e.preventDefault();
      e.stopPropagation();

      // Set focus on last question
      lastQuestion.focus();
      break;

    // Enter/Space
    case 13:
    case 32:
      e.preventDefault();
      e.stopPropagation();

      // Show answer content
      showAnswer(elem, thisSectionFaqQuestions);
      break;
    }

  };

  /**
   * On load, setup roles and initial properties
   */
  
  // Each FAQ Question
  faqQuestion.each(function (i) {
    $(this).attr({
      'id': 'faq-question-' + i,
      'role': 'tab',
      'aria-controls': 'faq-answer-' + i,
      'aria-expanded': 'false',
      'aria-selected': 'false',
      'tabindex': '-1'
    });
  });

  // Each FAQ Answer
  faqAnswer.each(function (i) {
    $(this).attr({
      'id': 'faq-answer-' + i,
      'role': 'tabpanel',
      'aria-labelledby': 'faq-question-' + i,
      'aria-hidden': 'true'
    });
  });

  // Each FAQ Section
  faqSection.each(function () {
    var $this = $(this),
      thisSectionFaqQuestions = $this.find('.js-faq-question');

    // Set section attributes
    $this.attr({
      'role': 'tablist',
      'aria-multiselectable': 'true'
    });

    thisSectionFaqQuestions.each(function (i) {
      var $this = $(this);

      // Make first tab clickable
      if (i === 0) {
        $this.attr('tabindex', '0');
      }

      // Click event
      $this.on('click', function () {
        showAnswer($(this), thisSectionFaqQuestions);
      });

      // Keydown event
      $this.on('keydown', function (e) {
        keyboardInteraction($(this), e, thisSectionFaqQuestions);
      });

      // Focus event
      $this.on('focus', function () {
        saveFocus($(this), thisSectionFaqQuestions);
      });
    });
  });

})(document, window  );

            
          
!
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.
Loading ..................

Console