A preferences form often lets people choose several items from the same list. Checkboxes in an HTML form let people turn each choice on or off independently.

A label lets people click either the checkbox or its text. A fieldset groups related choices under a single prompt, called a legend.

An unchecked checkbox contributes no entry when the form is submitted. In a GET form, the selected values appear in the URL query string.

Steps to add checkboxes to an HTML form:

  1. Create topics.html in your local test site's document root using your text editor, provided the filename is unused.
  2. Write the workshop topic form in topics.html.
    topics.html
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Workshop topics</title>
    </head>
    <body>
      <h1>Workshop topics</h1>
      <form action="topics.html" method="get">
        <button type="submit">Submit choices</button>
      </form>
    </body>
    </html>

    The form sends a GET request back to topics.html, so the local server can return the same page after submission.

  3. Insert the checkbox group inside the form immediately before the Submit choices button.
        <fieldset>
          <legend>Topics to explore</legend>
          <label>
            <input type="checkbox"
              name="topic" value="html"> HTML
          </label>
          <label>
            <input type="checkbox"
              name="topic" value="css"> CSS
          </label>
          <label>
            <input type="checkbox"
              name="topic" value="js"> JavaScript
          </label>
        </fieldset>

    The boxes share the field name topic and each has its own value. If two boxes are checked, the browser sends two entries under that name.

  4. Save topics.html.
  5. Open topics.html at http://localhost:8000/ in desktop Chrome.

    If your test server uses a different address or port, substitute it in the URL.

  6. Click the HTML label to check its box.
  7. Click the CSS label to check its box.
  8. Click Submit choices with only HTML and CSS checked.
  9. Verify that the returned Workshop topics page has topic=html&topic=css in its address.

    An error page, a missing value, or a topic=js entry means the check failed.

Share this guide