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

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:  
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:  
 
 
 
 
surindertarika1234
CSS-Misc
HTML-Misc
JavaScript-Misc
CSS
HTML
JavaScript
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to set space between the flexbox ?
Design a Tribute Page using HTML & CSS
Build a Survey Form using HTML and CSS
Form validation using jQuery
Design a web page using HTML and CSS
REST API (Introduction)
Hide or show elements in HTML using display property
How to set the default value for an HTML <select> element ?
How to set input type date in dd-mm-yyyy format using HTML ?
HTTP headers | Content-Type