liveBook · Manning

Option element

As explained in A Pythonic Adventure: From Python basics to a working web app

An option element is an HTML element representing one selectable choice within a select control.

An <option> element is one choice in a menu made with <select>. Put several options inside the select, and a visitor can pick a drink, flavor, or topping before submitting a form. Think of each option as one small label in the coffee-shop menu: Coffee, Chocolate, Decaf, and more. section 10.0 section 11.0

Start with the choices people should see. An option can display friendly text while its value is the shorter piece of data sent with the form. For example, Coffee can use the value coffee. A first option with an empty value can prompt the visitor to choose a drink. section 11.0 section 10.0

A drink menu

Place options between <select name="drink"> and </select>. When the form is posted to /order, the selected value is available as request.form["drink"], then can be displayed back in an order confirmation. section 11.0 section 10.0

Build a useful menu one choice at a time:

  • Add a prompt such as “- Choose drink -” so the menu does not begin with a real drink choice. section 11.0
  • Add an <option> for every available choice, such as coffee, chocolate, and decaf. section 11.0
  • Make separate selects when the order needs separate choices, such as flavor and topping. section 11.0
  • Indent options to make the HTML easier for people to read; HTML does not require that indentation. section 11.0
Listing 11.2 Additional menu options in templates/forms.html
{% extends "base.html" %}
{% block content %}
 
<form action="/order" method="post">
    <select name="drink">
      <option value="">- Choose drink -</option>
      <option value="coffee">Coffee</option>
      <option value="chocolate">Chocolate</option>
      <option value="decaf">Decaf</option>
    </select>
    <select name="flavor">
      <option value="">- Choose flavor -</option>
      <option value="caramel">Caramel</option>
      <option value="vanilla">Vanilla</option>
      <option value="peppermint">Peppermint</option>
      <option value="raspberry">Raspberry</option>
      <option value="plain">Plain</option>
    </select>
    <select name="topping">
      <option value="">- Choose topping -</option>
      <option value="chocolate">Chocolate</option>
      <option value="cinnamon">Cinnamon</option>
      <option value="caramel">Caramel</option>
    </select>
 
    <input type="submit" value="Submit">
</form>
 
{% endblock %}

Compare three select menus side by side: each one groups its own option elements for one part of an order. section 11.0

Try changing the menu yourself: add a new drink or replace the flavors with your own favorites. Then open the form in a browser, choose an option, submit it, and check that the selected value appears in the order result. section 10.0 section 11.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Explains option placement, values, readability, and form submission without detailing browser internals.
Examples
Uses Coffee, Decaf, Chocolate, flavors, toppings, and a submitted coffee-shop order.
What it assumes
Teaches option elements from a simple HTML form while assuming a browser and basic file editing.
What you can do after
Apply it via Flask forms to submit selected menu values and display an order.
How it connects
Connects <select>, <form>, input elements, POST requests, Flask request.form, and templates.

Also explained in

Compare how 2 books explain option element →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.