How to Read a Value From a Combo Box
JavaScript allows you to identify and manipulate a selected value in an HTML combo box. The combo box is the drop-down selection element that lets you standardize the list of options from which a reader can select. The "selectedValue" property lets you identify the selected value, and you can use the selected information to manipulate the page's content or redirect the user to another page.
Instructions
-
-
1
Right-click the HTML file that you want to edit and select "Open With." Click the HTML or JavaScript editor you want to use to edit the HTML page.
-
2
Create the JavaScript function to identify the selected value in the combo box. The following code displays the selected value in a function named "selectedValue":
function selectedValue(cb) {
alert ([cb.selectedIndex].value);
}
-
-
3
Link the combo box to the "selectedValue" function. The following code links the function to the combo box:
<select name="mycb" selectedindexchanged="selectedValue(this)">
<option selected value="blue">Blue</option>
<option value="brown">Brown</option>
</select>
In this example, when the user selects a value, the function is called and displays the value in the drop-down list.
-
1