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

The HTML document contains some option elements and the task is to get the text of options of the select elements by using its value with the help of JavaScript.
There are two approaches that are discussed below:
- Using value Property
- Using Object.values() Method
Method 1: Using value Property
First, select the options by JavaScript selector, and then use value Property (eg. option[i].value) to compare the values of the option element. If it’s a match then use text Property (eg. option[i].text) to get the text of the option element.
Example: This example shows the use of the above-explained approach.
html
| <!DOCTYPE html><html><head>    <title>        Get text of option tag by        value using pure JavaScript    </title></head><bodystyle="text-align:center;">    <h1style="color:green;">        zambiatek    </h1>    <h3>        Get text of option tag by        value using JavaScript    </h3>    <selectid='GFG_list'>        <optionvalue='GFG_1'>GFG_A</option>        <optionvalue='GFG_2'>GFG_B</option>        <optionvalue='GFG_3'>GFG_C</option>    </select>    <br><br>    <buttononclick="getText()">        Get Text by Value    </button>    <pid="GFG"></p>    <script>        let el = document.getElementById("GFG");            let val = "GFG_2";        function getText() {            let a = document.getElementById("GFG_list");            for (let i = 0; i < a.length; i++) {                let option= a.options[i];                if (option.value == val) {                    el.innerHTML= option.text;                }            }        }    </script></body></html> | 
Output:
 
Method 2: Using Object.values() Method
In this example, we can look for every option element and matches the specific value of an element. We are using the Object.values() method to get the values of the option element. Then apply the forEach() method on each option and alert the values along with their text.
Example: This example shows the use of the above-explained approach.
html
| <!DOCTYPE html><html><head>    <title>        Get text of option tag by        value using pure JavaScript    </title></head><bodystyle="text-align:center;">    <h1style="color:green;">        GeeksForGeeks    </h1>    <p>        Click on the button to        get the text of options    </p>    <selectid='GFG_list'>        <optionvalue='GFG_1'>GFG_A</option>        <optionvalue='GFG_2'>GFG_B</option>        <optionvalue='GFG_3'>GFG_C</option>    </select>    <br><br>    <buttonid="GFG_Button"onclick="getText()">        getText    </button>    <script>        function getText() {            Object.values(document.getElementById(                'GFG_list').options).                forEach(function (option) {                    alert("Value - " + option.value                        + ", Text - " + option.text);                });        }    </script></body></html> | 
Output:
 
How to get the text of option tag by value using JavaScript ?
 
				 
					


