Make an Accordion Exclusive

Your page from the last lesson has two questions as disclosures and two as headings. With all four as disclosures, Tomasz Nowak could open every answer and be back to a long page, so Kwame sorts them into groups.

In this lesson, you’ll make a group of questions close each other with one HTML attribute, decide which group should, and check it by keyboard and screen reader. Start by giving the page its two groups.

Tie Questions Together With name

Kwame’s version puts all four questions in two sections as disclosures, gives renewing its own question, and splits the late-return answer by situation. In how-borrowing-works.html, replace everything between the intro paragraph and </main> with his version:

Language: HTML
<section>
  <h2>Borrowing and Returning</h2>
  <details class="faq-item">
    <summary>How long can I keep a tool?</summary>
    <p>Seven days, counted from the day you pick it up.</p>
  </details>
  <details class="faq-item">
    <summary>Can I renew a tool?</summary>
    <p>Once, from My Reservations, if nobody has
      reserved it.</p>
  </details>
  <details class="faq-item">
    <summary>Can I borrow more than one tool?</summary>
    <p>Yes, up to three tools at once.</p>
  </details>
  <details class="faq-item">
    <summary>Do I need to clean a tool?</summary>
    <p>Brush off dirt and sawdust. We check, oil and
      sharpen every tool ourselves.</p>
  </details>
</section>
<section>
  <h2>Late Returns</h2>
  <details class="faq-item">
    <summary>Late, and the tool isn't reserved?</summary>
    <p>There's no fine. Renew it from My Reservations,
      or bring it back when you can.</p>
  </details>
  <details class="faq-item">
    <summary>Late, and the tool is reserved?</summary>
    <p>There's no fine, but bring it back today: a
      neighbor is waiting. You can't borrow another
      tool until it's back.</p>
  </details>
</section>

A member returning a tool late is in one of those two situations, and each answer stands alone, so one open answer shows them only what applies. A set of <details> elements where opening one closes the others is an exclusive accordion, and you make one by giving each element the same name attribute value. Add name="late-returns" to both Late Returns <details> tags:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.