Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 12 Oct, 2021

The HTML document 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: 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

html




<!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>

 

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: 
 

html




<!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>

 

Output: 
 

 

 

 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!