select object
Creates a drop-down list or list box element.
To add items to the select element, use the option tag.
The select tag allows simple or multiple selection, depending on the state of the multiple property.
- If multiple selection is allowed, the visual appearance of the select tag can only be a list box.
- If only one selected element is allowed, the select element may be a drop-down list or a list box. The default appearance is the drop-down list in that case.
The select element is one of the form controls.
The selected state of the control can be submitted to a server if the following conditions are met:
- A form element must contain the select element.
- The action property of the container form must be set to the URL of the server.
- The name property of the select element must be specified and non-empty. If multiple selection is allowed, use brackets [] at the end of the value to reach all of the selected elements on the server side.
- If multiple selection is allowed:
- If only one selected element is allowed:
- If only one selected element is allowed in the select element, the selectedIndex property contains the index of the selected item in the options collection of the select tag. If you need the value of the selected option in a single-selection list, you can use the value property of the select element.
- If multiple selection is allowed, you can get the selected elements by iterating throw the options collection of the select tag and check the value of the selected property of every option tag.
- If there is no selected element, the value of the selectedIndex property is -1.
If you need to detect when the selection has changed in a select element, use the onchange event. Example 6 demonstrates it.
Syntax:
Methods that return the object:
| document.createElement ("select") |
The base interface, through which you can add new functionalities to the select object, is the HTMLSelectElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: select |
Possible members:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Example HTML code 1:
This example illustrates the use of the select element for a single-selection drop-down list:
|
||||
<form method="post" action="#URL#"> My favorite fruit: <select name="fruit"> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> <br /><br /> <input type="submit" value="Send" /> </form> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example illustrates the use of the select element for a single-selection list box:
|
||||
<form method="post" action="#URL#"> My favorite fruit: <select name="fruit" size="3"> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> <br /><br /> <input type="submit" value="Send" /> </form> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example illustrates the use of the select element for a multiple-selection list:
|
||||
<form method="post" action="#URL#"> My favorite fruits: <select name="fruits[]" multiple="multiple"> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> <br /><br /> <input type="submit" value="Send" /> </form> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 4:
This example checks the selected item in a single-selection list, before submitting the form:
|
||||
<head> <script type="text/javascript"> function CheckAndSubmit () { var fruitForm = document.getElementById ("fruitForm"); var fruitList = document.getElementById ("fruitList"); if (fruitList.selectedIndex == 0) { alert ("Please select your favorite fruit!"); return; } fruitForm.submit (); } </script> </head> <body> <form id="fruitForm" method="post" action="#URL#"> My favorite fruit: <select name="fruit" id="fruitList"> <option value="">Please select a fruit</option> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> <br /><br /> <input type="button" value="Send" onclick="CheckAndSubmit ()" /> </form> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 5:
This example checks the count of the selected items in a multiple-selection list, before submitting the form:
|
||||
<head> <script type="text/javascript"> function CheckAndSubmit () { var fruitForm = document.getElementById ("fruitForm"); var fruitList = document.getElementById ("fruitList"); var selectedCount = 0; for (var i = 0; i < fruitList.options.length; i++) { if (fruitList.options[i].selected) { selectedCount++; } } if (selectedCount < 2) { alert ("Please select at least 2 pieces of fruit!"); return; } fruitForm.submit (); } </script> </head> <body> <form id="fruitForm" method="post" action="#URL#"> My favorite fruits:<br /> <select name="fruits[]" id="fruitList" multiple="multiple"> <option value="Apple">Apple</option> <option value="Pear">Pear</option> <option value="Peach">Peach</option> </select> <br /><br /> <input type="button" value="Send" onclick="CheckAndSubmit ()" /> </form> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 6:
This example illustrates the use of the onchange event for a select element:
|
||||
<head> <script type="text/javascript"> function OnSelectionChange (select) { alert ("The selected option is " + select.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 7:
This example displays the text of 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 8:
This example is similar to the previous one but it displays the value of 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.value; } } 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 value="Apple">Apple</option> <option value="Peach">Peach</option> <option value="Pear">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 9:
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 10:
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 11:
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 12:
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