Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Select lists are another common feature for selecting from a list of options. Let's make a working copy of select.html, and we'll see how this works. I'll rename this to select-working.html and open it up my text editor. So you notice up here at the top we are using the same forms.css and forms.js that we've been using throughout this chapter. And here we have our actual form, and you'll notice that this select element is actually implemented with a select tag.
It doesn't overload the input tag like so many of the other elements in HTML forms do. The select tag is a container, and it contains option tags, and these option tags are also containers. You notice they have end tags, and they have content, and that content is the text label for the dropdown list. Let's take a look at the dropdown list. We are going to open this in a browser. And in this case, I'm going to use the Firefox browser. This particular element works in all the different browsers. And here's this text. It says "Select a Cat," and you'll notice that the first element in my list says "Select a Cat." And it has the selected attribute, which means that it will always be selected upon startup, and it also says disabled, which means that you cannot actually select it.
So you will notice that as I move my cursor here, I can't get back to Select a Cat. I have to select one of the other cats, and that's because it says disabled. And you notice that each of these cats--these are all fictional cats from literature and other sources-- and you'll notice that they have the name of the cat inside the content of the option and then a short name as the value. So when I select one of these and I hit the Big Red Button, you notice that the value comes up just saying hat.
So The Cat in the Hat, his value is just hat. Now if I delete The Cat in the Hat text from the option, then when I reload this, you will notice that it comes up with an empty spot there. In older browsers, it would actually use the short name in the selection list. That's not always the case though, so you want to make sure that you always have some content here for your selection list. And there it's back, and it says The Cat in the Hat. There are two distinct ways that this list can be formatted.
There is the dropdown list like this that we are all familiar with, and if I put in a size here, if I say size="5" and save this and reload it in the browser, now it actually gets formatted as a scrolling list instead of a dropdown list. And I can still select anything I want to from the list. There's also a multiple attribute, which will allow me to select multiple items from the list. If I put in that multiple attribute and I go ahead and reload this, now I can--on my Mac I am holding down the Command key; on a PC or another platform you usually hold down the Ctrl key--and you can select multiple items.
And when you're using the scrolling list, you probably most of the time, are not going to want this disabled selected item at the top. So I will go ahead and delete that for now, and we can reload this, and we see that that's gone. One more feature that you need to know about here: when your lists get very long, sometimes you are going to want to segment them, and so there's an optgroup element which is useful for that, and it looks like this. I say
So I might want to have another section for the cats from film and another one for the cats from television and another one for cats from comics. I would like to do a little bit of formatting. Even when I am doing something that I think is going to be temporary, I have gotten to the place where I just format it anyway, because now and then these things end up being permanent and I come back and it's formatted all wacky and it's just not that hard to do. So I will go ahead and I will save this and I'll bring this up in the browser, and you notice that our cats are now in groups: Literature, Film, Television and Comics.
If the size were bigger, say size="6", save that and reload, and now that's starting to look really good. It also looks good in the dropdown version. Save that and reload and we can see that we have all of our cats segmented in our optgroups. So the select list is another feature that's been around since the beginning of the HTML forms interface. This feature is supported by all browsers that support forms.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98470 Viewers
61 Video lessons · 85754 Viewers
71 Video lessons · 69656 Viewers
56 Video lessons · 101986 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.