HTML | DOM Option Object

The DOM Option Object is used to represent the HTML <option> element. The Option element is accessed by getElementById().

Properties:

  • disabled: This attribute contains the value disabled which represents option is disabled.
  • label: This attribute contains the text value which represents the shorted label for the option.
  • selected: This attribute contains the value selected which represents the item is pre-selected when the browser loaded.
  • value: This attribute contains the value text which sent to the server.

Syntax:

document.getElementById("ID");

Where “id” is the ID assigned to the “Option” tag.

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title> DOM Option Object</title>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM Option Object</h2>
    <select>
        <option>Choose an option</option>
          
        <!-- Assigning id to option tag. -->
        <option id="GFG" value="html">HTML</option>
        <option value="java">JAVA</option>
        <option value="C++">C++</option>
        <option value="php">PHP</option>
        <option value="perl">PERL</option>
    </select>
    <br>
    <br>
    <button onclick="geeks()">Submit</button>
    <p id="sudo"></p>
    <script>
        function geeks() {
            
            //  Accessing option object.
            var g = document.getElementById(
              "GFG").text;
            document.getElementById("sudo").innerHTML = g;
        }
    </script>
</body>
  
</html>

chevron_right


:

Output:

Before Clicking On Button:

After Clicking On Button:

Example-2: Option Object can be created by using the document.createElement Method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title> DOM Option Object</title>
    <style>
        body {
            text-align: center;
        }
          
        h1 {
            color: green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM Option Object</h2>
    <select id="GFG">
    </select>
    <br>
    <br>
    <button onclick="geeks()">Submit</button>
    <p id="sudo"></p>
    <script>
        function geeks() {
            
            //  Creating option object.
            var g = document.createElement("OPTION");
            
            var f = document.createTextNode(
              "Choose an Option");
            
            g.appendChild(f);
            document.getElementById("GFG").appendChild(g);
            var x = document.createElement("OPTION");
            x.setAttribute("value", "HTML");
            var t = document.createTextNode("html");
            x.appendChild(t);
            document.getElementById("GFG").appendChild(x);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Before Clicking On Button:

After Clicking On Button:

Supported Browsers: The browser supported by DOM Option Object are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.