HTML: Selection, Popup Menu

By Xah Lee. Date: . Last updated: .

Single Value Selection (Popup Menu)

Result:

<div>
<select id="pets3" name="pets3" size="1">
<option value="dog">dog</option>
<option value="cat" selected>cat</option>
<option value="bird">bird</option>
</select>
</div>

<p>Result:
<span id="showbox55700"></span>
</p>

Size attribute must 1, otherwise it's a selection-list and returns a list.

{
    const selectBox = document.getElementById('pets3');
    const showBox = document.getElementById('showbox55700');
    const f_update = (() => {
        showBox.textContent = selectBox.value;
    });
    selectBox.addEventListener('input', f_update, false);
    f_update();
}

Multiple Values Selection

select tag can have attribute multiple, which allow user to make more than one selection.

Hold down Ctrl to select more than one item. (on the Mac, hold down ⌘ command.)

Result:

<select id="animals" name="animals" size="6" multiple>
 <option value="dog">dog
 <option value="cat">cat
 <option value="bird">bird
 <option value="pig">pig
 <option value="rabbit">rabbit
 <option value="snake">snake
</select>

<p>Result:
<span id="showbox03892"></span>
</p>
{
    const aniSelect = document.getElementById('animals');
    const showBox = document.getElementById('showbox03892');
    const f_update2 = (() => {
        const selectedValues = Array.from(aniSelect.options)
            .filter(((x) => x.selected)).map(((x) => x.value));
        showBox.textContent = selectedValues.toString();
    });
    aniSelect.addEventListener('input', f_update2, false);
    f_update2();
}

Web Scripting Overview

  1. Browser Window Object, DOM
  2. Intro to Event-Based Programing
  3. JavaScript Load Order

DOM How-To

  1. Basic DOM Methods
  2. Get Element by ID, Tag, Name, Class, CSS
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Attribute Value
  8. Set Attribute Value
  9. Remove Attribute
  10. List/Add/Remove Class Attribute
  11. Add/Remove Event Handler
  12. Navigate DOM Tree
  13. Node Type, Name, Value

HTML Input

  1. Button
  2. Text Field
  3. Password Field
  4. Textarea
  5. Email 📧
  6. Number Field
  7. Radio Button 🔘
  8. Checkbox 🗹
  9. Popup Menu
  10. Slider 🎚
  11. Color Picker 🌈

Web Scripting Examples

  1. Disable Right Click
  2. document.write
  3. Image Rollover
  4. Pop-up New Window
  5. Digital Clock
  6. Stopwatch
  7. Fade a Element
  8. How to Create Tooltip
  9. Falling Snow Effect
  10. Box Following Scroll

Web Scripting Misc

  1. Web Cookies
  2. Web Storage
  3. DOM: Open URL window.open
  4. Encode URL, Escape String
  5. Browser Info, Navigator Object
  6. Get URL (window.location)
  7. Find Window Width
  8. Find Element Width
  9. WebSocket
  10. Value of “this” in Event Handler
  11. Event Delegation
  12. Get Current Script Tag
  13. insertAfter Element
  14. Randomize List
  15. create Document Fragment
  16. innerHTML, nodeValue, textContent, innerText?
  17. What is Live Object
  18. NodeList vs HTMLCollection
  19. Whitespace Nodes

jQuery

  1. jQuery Basics
  2. jQuery() vs querySelectorAll()
  3. Write JQuery Plugin

node.js

Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials