- By using the value property
- By using selectedIndex property with the option property
We will understand both these methods through examples.
Method 1: Using the value property:
The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
Example: This example describes the value property that can be found for the selected elements.
The selectedIndex property returns the index of the currently selected element in the dropdown list. This index starts from 0 and returns -1 if no option is selected. The options property returns the collection of all the option elements in the <select> dropdown list. The elements are sorted according to the source code of the page. The index found before it can be used with this property to get the selected element. This option’s value can be found by using the value property.
- selectedIndex: It is used to set or get the index of the selected <option> element in the collection.
- length: It is read-only property that is used to get the number of <option> elements in the collection.
Return value: It returns HTMLOptionsCollection Object by specifying all the <option> elements in the <select> element. The element will be sorted in the collection
Example: This example describes the selectedIndex property with the option property.