options collection
The elements in the collection are sorted in source code order.
Syntax:
The base interface, through which you can add new functionalities to the options collection, is the HTMLOptionsCollection interface.
Possible members:
Properties:
Returns an integer that specifies the number of elements in the current collection.
This property is read-only. |
|||||||
Specifies or returns the zero-based index of the selected option in a select object or in the selection list of a keygen object. |
Methods:
[nameOrIndex] |
Returns an element or a collection of elements from the current collection by name or index.
Parameters:
Return value:
|
||||||||||||||||||||||
add (element [, index]) |
Inserts an option element into the current collection at the specified position.
Parameters:
Return value:
This method has no return value.
|
||||||||||||||||||||||
item (nameOrIndex [, subIndex]) |
Returns an element or a collection of elements from the current collection by name or index.
Parameters:
Return value:
|
||||||||||||||||||||||
namedItem (name) |
Returns an element or a collection of elements from the current collection by name.
Parameters:
Return value:
|
||||||||||||||||||||||
remove (index) |
Removes the option element at the specified position from the current collection.
Since Firefox does not support this method, for a cross-browser solution, use the remove method of the select element to remove an option by position and use the removeChild method to remove an option element by reference.
Parameters:
Return value:
This method has no return value.
|
||||||||||||||||||||||
tags | Returns a NodeList collection that contains all elements from the current collection with the specified tag name. | ||||||||||||||||||||||
urns | Returns a NodeList collection that contains all elements to which the specified behavior is attached. |
Example HTML code 1:
This example illustrates the use of the options collection for a single-selection list:
|
||||
<head> <script type="text/javascript"> function GetSelOption (selectTag) { var selIdx = selectTag.selectedIndex; var selOption = selectTag.options[selIdx]; alert ("The selected option is " + selOption.text); } </script> </head> <body> Change the selection in the list below: <select onchange="GetSelOption (this);"> <option>Apple</option> <option>Peach</option> <option>Pear</option> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example displays the selected items in a multiple-selection list:
|
||||
<head> <script type="text/javascript"> function GetSelected (selectTag) { var selIndexes = ""; for (var i = 0; i < selectTag.options.length; i++) { var optionTag = selectTag.options[i]; if (optionTag.selected) { if (selIndexes.length > 0) selIndexes += ", "; selIndexes += optionTag.text; } } var info = document.getElementById ("info"); if (selIndexes.length > 0) { info.innerHTML = "Selected options: " + selIndexes; } else { info.innerHTML = "There is no selected option"; } } </script> </head> <body> Please select some options (use the CTRL key for multiple selection) <br /> <select multiple="multiple" onchange="GetSelected (this);"> <option>Apple</option> <option>Peach</option> <option>Pear</option> </select> <br /><br /> <span id="info" style="border:1px solid #606060; background-color:#e0d0e0;"></span> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example illustrates the use of the add method:
|
||||
<head> <script type="text/javascript"> function InsertOption () { var selectTag = document.getElementById ("mySelect"); var option = document.createElement ("option"); option.text = "second"; selectTag.options.add (option, 1); } </script> </head> <body> <select id="mySelect" size="5"> <option>first</option> <option>third</option> </select> <button onclick="InsertOption ()">Insert an option</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 4:
|
||||
<head> <script type="text/javascript"> function OnSelectionChange (select) { var selectedOption = select.options[select.selectedIndex]; alert ("The selected option is " + selectedOption.value); } </script> </head> <body> Select an item from the following list:<br /> <select onchange="OnSelectionChange (this)"> <option value="Apple" />Apple <option value="Pear" />Pear <option value="Peach" />Peach </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 5:
This example shows how to create a selection list dynamically:
|
||||
<head> <script type="text/javascript"> var countries = ['Australia', 'Great Britain', 'Germany', 'United States']; function FillCountryList () { var countryList = document.getElementById ("country"); for (var i=0; i < countries.length; i++) { // Option (text, value) var countryOption = new Option (countries[i], countries[i]); countryList.options.add (countryOption); } } </script> </head> <body onload="FillCountryList ()"> <select id="country"> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 6:
This example is similar to the previous one, but it uses 2-character alphabetical country codes for option values. It is useful if you want to store the selected country in database.
|
||||
<head> <script type="text/javascript"> var countries = ['Australia', 'AU', 'Great Britain', 'GB', 'Germany', 'DE', 'United States', 'US']; function FillCountryList () { var countryList = document.getElementById ("country"); for (var i=0; i < countries.length; i += 2) { // Option (text, value) var countryOption = new Option (countries[i], countries[i+1]); countryList.options.add (countryOption); } } </script> </head> <body onload="FillCountryList ()"> <select id="country"> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 7:
The previous two examples create selection list from arrays. The following one creates a selection list from a string. It is useful if you use AJAX to fill the contents of a selection list. For further details about AJAX, see the page for the XMLHttpRequest object and the responseText property.
|
||||
<head> <script type="text/javascript"> // the response of an AJAX call (responseText) var httpResponse = "Australia,AU,Great Britain,GB,Germany,DE,United States,US"; var countries = httpResponse.split (","); function FillCountryList () { var countryList = document.getElementById ("country"); for (var i=0; i < countries.length; i += 2) { // Option (text, value) var countryOption = new Option (countries[i], countries[i+1]); countryList.options.add (countryOption); } } </script> </head> <body onload="FillCountryList ()"> <select id="country"> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 8:
This example illustrates how you can change the items in a select element depending on the selected item in another select element:
|
||||
<head> <script type="text/javascript"> var countriesAndCities = {}; countriesAndCities['AU'] = ['Brisbane', 'Melbourne', 'Sydney']; countriesAndCities['GB'] = ['Liverpool', 'London', 'Manchester']; countriesAndCities['DE'] = ['Berlin', 'Hamburg', 'Munich']; countriesAndCities['US'] = ['Chicago', 'Los Angeles', 'New York']; function ChangeCityDropList () { var countryDropList = document.getElementById ("country"); var cityDropList = document.getElementById ("city"); var selCountry = countryDropList.options[countryDropList.selectedIndex].value; // remove all cities while (cityDropList.options.length) { cityDropList.remove (0); } // add new cities var cities = countriesAndCities[selCountry]; if (cities) { for (var i=0; i < cities.length; i++) { var city = new Option (cities[i], i); cityDropList.options.add (city); } } } </script> </head> <body onload="ChangeCityDropList ();"> <select id="country" onchange="ChangeCityDropList();"> <option value="">-- Country --</option> <option value="AU">Australia</option> <option value="GB">Great Britain</option> <option value="DE" selected="selected">Germany</option> <option value="US">United States</option> </select> <select id="city"> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
External links:
User Contributed Comments