Table of Contents

2.3.3. Simulated drop-down lists

Purpose

Drop-down lists referred to as “simulated” only use some of the default HTML form elements for drop-down lists, namely, the <select> fields and <option> tags. Simulated drop-down lists make use of a useful feature of CSS that makes it possible to customize a part of lists obtained.

Note

There are other more advanced techniques to simulate drop-down lists in their entirety, which give you the tools to fine tune the appearance of choice lists. These techniques are much more complicated than the solution proposed here, and are not discussed in this document.

Basic HTML Code

The basic HTML code for a standard drop-down list:

<form action="index.html" method="post">
    <label for="eye-colour">
        Which colour are your eyes?
        <select name="eye-colour" id="eye-colour">
            <option value="black">Black</option>
            <option value="blue">Blue</option>
            <option value="green">Green</option>
            <option value="brown">Brown</option>
            <option value="other">Other</option>
        </select>
    </label>
 
    <input type="submit" value="Submit information" />
</form>

Without JavaScript, a standard form is shown to the user.

Behaviour with JavaScript

With JavaScript, the behaviour of simulated drop-down lists must be the same as those of standard drop-down lists.

To implement the essentials of this method, you need to respect the following steps:

  1. The CSS property opacity: 0; is applied to the <select> tag when JavaScript is active. This property makes the list invisible by default, but the options are displayed when you click <select>. The list stays in the same place on the screen, since it is only made invisible by changing the opacity.
  2. An empty container is created in the DOM, just after the <select>. For example, a <span> can be used. This container is then dynamically populated in JavaScript, with the current value of the drop-down list. The script is called each time the status of the drop-down list changes.
  3. The invisible <select> is positioned in the CSS just above the created container, so that when you click on the container it triggers the opening of the drop-down list.

Going into more detail, the scripts must handle the following points:

The resulting code in JavaScript is as follows:

<form method="post" action="index.html">
    <label for="eye-colour">
        Which colour are your eyes ?
        <div class="list-container">
            <select id="eye-colour" name="eye-colour">
                <option value="black">Black</option>
                <option value="blue">Blue</option>
                <option value="green">Green</option>
                <option value="brown">Brown</option>
                <option value="other">Other</option>
            </select>
            <span class="container-choice" aria-hidden="true">Black<img alt="" src="images/arrow.png"></span>
        </div>
    </label>
 
    <input type="submit" value="Submit information">
</form>

Démonstration