Open In App

HTML DOM Select options Collection

The Select Options Collection in HTML DOM is used to set or return the collection of all options elements in a Drop-Down list. The elements are presented in a sorted order as they appear in a Source Code.

 



Syntax:

selectObject.options

 



Properties:

Methods: The DOM select option collection contains five methods which are given below:

Example 1: Below HTML code used to return the number of options in a drop-down list. 




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green;font-size:39px;">
            GeeksForGeeks
        </h1>
  
        <h2>DOM Select Option Collection</h2>
          
        <label>Your favourite Food Dish: </label>
        <select id="dish">
            <option>rajma rice</option>
            <option>Idli sambhar </option>
            <option>Samosa</option>
            <option>Daal rice</option>
            <option> shahi paneer </option>
        </select>
        <br>
  
        <button onclick="myGeeks()">Submit</button>
        <p id="sudo" style="font-size:20px;color:green;"></p>
  
        <script>
            function myGeeks() {
                var g = document.getElementById(
                        "dish").options.length;
  
                document.getElementById("sudo")
                    .innerHTML = "The are " + g 
                    + " options in the drop-down list";
            }
        </script>
    </center>
</body>
  
</html>

Output:

Example 2: Below HTML code used to get a value of the options which is at 2nd options. 




<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green;font-size:39px;">
            GeeksForGeeks
        </h1>
  
        <h2>DOM Select Option Collection</h2>
          
        <label>Your favourite Food Dish: </label>
        <select id="dish">
            <option>rajma rice</option>
            <option>Idli sambhar </option>
            <option>Samosa</option>
            <option>Daal rice</option>
            <option> shahi paneer </option>
        </select>
        <br>
  
        <button onclick="myGeeks()">Submit</button>
        <p id="sudo" style="font-size:20px;color:green;"></p>
  
        <script>
            function myGeeks() {
                var g = document.getElementById(
                        "dish").options[1].text;
  
                document.getElementById("sudo").innerHTML 
                    = "The second options is :" + g;
            }
        </script>
    </center>
</body>
  
</html>

Output:

Supported Browsers:


Article Tags :