option object | Option object
Option | ||||||
option |
Specifies an item in a selection list.
The option element is used to add items to a select element.
If the select element that contains the option tag is placed within a submittable form,
and the form is submitted, the contents of the value property of the selected option(s) will be sent to the server.
If the value property is not specified, the text content of the option tag will be sent.
You can set the initial selected state with the SELECTED attribute.
For further details, see the page for the select tag.Do not use the onclick event on an option to detect when the option has been selected.
If you need to detect when an option has been selected or when the selection has changed in a select element, use the onchange event on the select element.
Note that the support of several events (such as onclick, onmousedown, onmousemove, etc.) depends on the appearance of the select element.
They are supported on list boxes (except in Internet Explorer), but only Firefox supports all of them on drop-down lists.
Syntax:
Methods that return the object:
| document.createElement ("option") |
| new Option (text, value) |
| options.item (nameOrIndex [, subIndex]) |
| select.item (index [, subIndex]) |
| options.namedItem (name) |
| select.namedItem (name) |
The base interface, through which you can add new functionalities to the option object, is the HTMLOptionElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: option |
Possible members:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Example HTML code 1:
This example illustrates the use of the option element:
|
||||
<select> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates a multiple list control:
|
||||
<select size="3" multiple="multiple"> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
|
||||
<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 4:
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 5:
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 6:
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 7:
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?
|
Related pages:
External links:
User Contributed Comments