You can also see the article for creating the dropdown list how to create the dropdown menu from this article.
We can get the values using 2 methods:
- By using the value property
- By using the 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 valuable property that can be found for the selected elements.
Method 2: Using the selectedIndex property with the option property:
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 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 a read-only property 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.
Please Login to comment...