JavaScript Listbox and Dropdown Select

A JavaScript listbox is usually an HTML <select> element. A normal select lets the visitor choose one predefined option; adding the multiple attribute allows more than one selection. JavaScript can read the selected value, validate it, enable or disable the control, add or remove options, build dependent lists, and respond to the change event.

<label for="language">Choose a language</label>
<select id="language" name="language">
  <option value="">Select one</option>
  <option value="PHP">PHP</option>
  <option value="JavaScript">JavaScript</option>
  <option value="Python">Python</option>
</select>

The selected value is available through the element’s value property:

const language = document.getElementById("language");
console.log(language.value);

For the HTML-only foundation, see HTML dropdown list boxes.

Table of Contents

Create and Read a Select Element Top ↑

Use a <label> connected with for/id, give each option a useful value, and include a blank placeholder when the form requires an explicit choice.

const list = document.getElementById("language");
list.addEventListener("change", () => {
  console.log(list.value);
});

Useful Select and Option Properties Top ↑

Property / APIUse
select.valueValue of the first selected option.
select.selectedIndexIndex of the selected option, or -1 when no option is selected.
select.optionsLive collection of all options.
select.selectedOptionsCollection of selected options; especially useful with multiple.
select.disabledEnable or disable the complete list.
option.selectedRead or change one option’s selected state.
new Option(text, value)Create an option in JavaScript.

Single-Selection Listbox Examples Top ↑

Validate a listbox before form submission, enable or disable a listbox, or respond to the user with the change event.

Multiple Selection Top ↑

Use multiple when more than one option is valid. Read the choices from selectedOptions and convert the collection to an array when you want array methods.

const selectedValues = Array.from(list.selectedOptions, option => option.value);

See reading multiple selections and limiting the number of selections.

Dynamic Option Management Top ↑

The original listbox series also covers adding options, removing options, moving options between lists, and copying listbox data with other form fields.

Dependent Dropdowns and Data Top ↑

Dependent dropdowns change the second list from the first selection. The related database-backed dropdown tutorial shows how table data can supply those options, while dropdown redirection uses a selected value to choose a destination.

JavaScript Form Validation




Subscribe to our YouTube Channel here



plus2net.com







srinivas

29-03-2010

It is very good site for learners, who wants work with the php,asp etc.., can u provide more content to learners



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer