Coding Lesson

← All lessons

JavaScript: Interaction

Show and hide a tip

Lesson 15 of 18 · About 20 minutes

Your goal: Add an accessible interaction to your hobby page.

Learn

A show-and-hide control has three parts that must agree: the content’s visibility, the button’s visible label, and its expanded state for assistive technology. The starter handles visibility and aria-expanded. Your task adds a label that describes what the next click will do.

Walkthrough: why the code works

  1. Read the starting state

    The tip paragraph has hidden, so it starts out of view. The button says Show tip and has aria-expanded="false". Its aria-controls="tip" points to the paragraph’s id. These attributes describe the relationship; JavaScript still performs the change.

  2. Follow the existing toggle

    The script stores references to the button and paragraph using const. These references stay the same even though the elements’ properties change. On each click, tip.hidden = !tip.hidden reverses the Boolean hidden value: true becomes false and false becomes true.

  3. Keep the expanded state aligned

    After toggling, !tip.hidden is true when the tip is visible. String(...) turns that Boolean into the attribute text "true" or "false". The existing setAttribute line keeps aria-expanded in sync. Do not remove it when adding the visible label.

  4. Choose the label after changing visibility

    Add this condition inside the click function after the visibility and expanded-state updates. When the tip is hidden, the next action is Show tip. When it is visible, the next action is Hide tip. Lesson 14’s if/else pattern is useful here.

    if (tip.hidden) {
      button.textContent = "Show tip";
    } else {
      button.textContent = "Hide tip";
    }
  5. Integrate and test repeatedly

    Change the tip to something useful for your hobby, then copy the button, paragraph and script into your project. Avoid duplicate ids already used by another control. Test show, hide and show again with mouse and keyboard. Hidden content is still delivered to the browser; this is a display feature, not a way to protect private information.

Expected results

  • On first load the tip is hidden and the button says Show tip.

  • After one activation the tip is visible, the label is Hide tip and aria-expanded is true.

  • After a second activation the tip is hidden, the label is Show tip and aria-expanded is false.

Common mistakes and how to fix them

  • The label describes the previous state: Set the label after changing tip.hidden. Base the wording on the action the next click will perform.

  • hidden="false" still hides the tip: hidden is a Boolean HTML attribute. For this exercise remove the attribute to start visible, or assign false to the hidden JavaScript property.

  • The wrong tip changes: Give each id a unique value and update getElementById and aria-controls when integrating the example.

View starter code
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My hobby page</title>
    <style></style>
  </head>

  <body>
    <h1>My hobby</h1>
    <button id="toggle" aria-expanded="false" aria-controls="tip">Show tip</button>
    <p id="tip" hidden>Practise regularly.</p>
    <script>
      const button = document.getElementById("toggle");
      const tip = document.getElementById("tip");
      button.addEventListener("click", function () {
        tip.hidden = !tip.hidden;
        button.setAttribute("aria-expanded", String(!tip.hidden));
      });
    </script>
  </body>
</html>

Your challenge

Change the tip and make the button label switch between Show tip and Hide tip. Then add this interaction to your hobby page.

Keep developing your hobby page: Download HTML before leaving the sandbox, then choose Open HTML file when returning to your project. Load saved draft restores only this lesson’s draft in this browser.

Practise in the sandbox
Show a hint

After changing tip.hidden, use a condition to set the button’s textContent.

Show a sample answer

Try the challenge first. This is one possible answer or a snippet to adapt.

// Add inside the click listener, after toggling tip.hidden:
if (tip.hidden) {
  button.textContent = "Show tip";
} else {
  button.textContent = "Hide tip";
}

Check your work

Repeated clicks show and hide the tip; the label describes the next action.

Completion is your own check, not an automatic grade.