New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating selection lists and dropdown lists

From: HTML Essential Training

Video: Creating selection lists and dropdown lists

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.

Creating selection lists and dropdown lists

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.

When I press the Big Red Button here, you see I get select cheshire, bigglesworth, cat. It's actually sending to the JavaScript an array with multiple values. And with CGI, it will send complete separate elements with a name value pair with same name. One other interesting feature of the multiple is I can take out this size now and it will always default to the scrolling list, and it'll default to a size of four. So, because I have multiple selected, multiple just doesn't work very well with the dropdown interface.

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 And then down at the end of that section, I close the optgroup, and now all of these options inside of that optgroup container are in a section.

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.

Show transcript

This video is part of

Image for HTML Essential Training
HTML Essential Training

82 video lessons · 94600 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 5m 24s
    1. Welcome
      56s
    2. Using the exercise files
      1m 37s
    3. What you need to know about this course
      2m 51s
  2. 22m 0s
    1. What is HTML?
      4m 12s
    2. Examining the structure of an HTML document
      7m 50s
    3. Understanding tags and containers
      6m 4s
    4. Exploring content models in HTML5
      2m 23s
    5. Looking at obsolete elements
      1m 31s
  3. 27m 19s
    1. Understanding whitespace and comments
      3m 53s
    2. Displaying text with paragraphs
      3m 37s
    3. Applying style
      8m 5s
    4. Using block and inline tags
      6m 34s
    5. Displaying characters with references
      5m 10s
  4. 16m 36s
    1. Exploring the front matter of HTML
      2m 9s
    2. Applying CSS to your document
      3m 59s
    3. Adding scripting elements
      4m 54s
    4. Using the meta tag
      3m 34s
    5. Optimizing your page for search engines
      2m 0s
  5. 24m 59s
    1. Controlling line breaks and spaces
      2m 46s
    2. Exploring phrase elements
      1m 44s
    3. Using font markup elements
      1m 5s
    4. Highlighting text with mark
      1m 29s
    5. Adding headings
      1m 38s
    6. Using quotations and quote marks
      3m 2s
    7. Exploring preformatted text
      1m 45s
    8. Formatting lists
      2m 28s
    9. Forcing text direction
      3m 49s
    10. Suggesting word-break opportunities
      2m 29s
    11. Annotating East Asian languages
      2m 44s
  6. 29m 15s
    1. Introducing CSS
      55s
    2. Understanding CSS placement
      6m 55s
    3. Exploring CSS syntax
      10m 34s
    4. Understanding CSS units of measure
      3m 3s
    5. Some CSS examples
      7m 48s
  7. 22m 5s
    1. Using images
      4m 13s
    2. Flowing text around an image
      4m 55s
    3. Breaking lines around an image
      3m 3s
    4. Aligning images
      5m 25s
    5. Mapping links in an image
      4m 29s
  8. 22m 28s
    1. Understanding URLs
      2m 41s
    2. Working with hyperlinks
      3m 28s
    3. Using relative URLs
      4m 20s
    4. Specifying a base URL
      2m 19s
    5. Linking within a page
      4m 12s
    6. Using image links
      5m 28s
  9. 17m 2s
    1. Exploring list types
      3m 52s
    2. List elements in depth
      7m 44s
    3. Using text menus with unordered lists
      5m 26s
  10. 15m 30s
    1. Introduction to HTML semantics
      4m 9s
    2. Exploring an example
      4m 56s
    3. Marking up figures and illustrations
      2m 33s
    4. Creating collapsible details
      3m 52s
  11. 11m 18s
    1. Embedding audio
      5m 19s
    2. Embedding video
      5m 59s
  12. 11m 53s
    1. Creating ad-hoc Document Object Model (DOM) data with the data-* attribute
      4m 53s
    2. Displaying relative values with meter
      2m 57s
    3. Creating dynamic progress indicators
      4m 3s
  13. 4m 49s
    1. Overview of HTML5 microdata
      1m 8s
    2. Exploring an example with microdata
      3m 41s
  14. 7m 3s
    1. Understanding outlines
      52s
    2. A demonstration of outlining
      6m 11s
  15. 13m 1s
    1. Table basics
      7m 29s
    2. Exploring the semantic parts of a table
      2m 32s
    3. Grouping columns
      3m 0s
  16. 9m 55s
    1. Frames overview
      54s
    2. Using traditional frames
      4m 26s
    3. Exploring inline frames using iframe
      2m 7s
    4. Simulating frames with CSS
      2m 28s
  17. 53m 7s
    1. Introducing forms
      10m 24s
    2. Using text elements
      10m 12s
    3. Using checkboxes and radio buttons
      2m 37s
    4. Creating selection lists and dropdown lists
      5m 14s
    5. Submit and button elements
      8m 48s
    6. Using an image as a submit button
      2m 15s
    7. Keeping context with the hidden element
      3m 0s
    8. Setting tab order
      2m 7s
    9. Preloading an autocomplete list using the datalist feature
      5m 26s
    10. Displaying results with output
      3m 4s
  18. 19m 47s
    1. Touring a complete site
      2m 14s
    2. Touring the HTML
      8m 44s
    3. Touring the CSS
      8m 49s
  19. 29s
    1. Goodbye
      29s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.