How to get the text of option tag by value using JavaScript ?

The HTML documment containing some option elements and the task is to get the text of options of select element by using its value with the help of JavaScript. There are two approaches that are discussed below:

Approach 1: First, select the options by JavaScript selector, Use value Property (eg. option[i].value) to compare the values of option element. If it’s a match then use text Property (eg. option[i].text) to get the text of option element.

  • Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Get text of option tag by
            value using pure JavaScript
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
          
        <p id="GFG_UP"></p>
          
        <select id='GFG_list'>
            <option value='GFG_1'>GFG_A</option>
            <option value='GFG_2'>GFG_B</option>
            <option value='GFG_3'>GFG_C</option>
        </select>
          
        <br><br>
      
        <button id="GFG_Button" onclick="getText()">
            getText
        </button>
      
        <p id="GFG_P"></p>
      
        <script>
            var el = document.getElementById("GFG_P");
            var up = document.getElementById("GFG_UP");
            var val = "GFG_2";
      
            up.innerHTML = "Click on the button to get "
                + "the text of option with, Value - " + val;
                  
            function getText() {
                var a = document.getElementById("GFG_list");
                for (var i = 0; i < a.length; i++) {
                    var option = a.options[i];
                    if (option.value == val) {
                        el.innerHTML = option.text;
                    }
                }
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

Approach 2: In this example, we can look for the every option element and matches for the specific value of element. We are using Object.values() method to get the values of the option element. Then apply forEach() method on each option and alert the values along with their text.

  • Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            Get text of option tag by 
            value using pure JavaScript
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
          
        <select id='GFG_list'>
            <option value='GFG_1'>GFG_A</option>
            <option value='GFG_2'>GFG_B</option>
            <option value='GFG_3'>GFG_C</option>
        </select>
        <br><br>
      
        <button id="GFG_Button" onclick="getText()">
            getText
        </button>
      
        <script>
            var up = document.getElementById("GFG_UP");
            up.innerHTML = "Click on the button to "
                        + "get the text of options";
      
            function getText() {
                Object.values(document.getElementById(
                    'GFG_list').options).
                    forEach(function (option) {
                        alert("Value - " + option.value
                             + ", Text - " + option.text);
                    });
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:


full-stack-img




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.