Skip to content

All topics  /  JavaScript

How to Get Value of Selected Option In Javascript?

JavaScript

When “How to Get Value of Selected Option In Javascript?” moves through design, development and browser testing, the platform page can clarify hand-offs and time spent on revisions while leaving code quality, accessibility and released behaviour as the real measures of success.

For API changes, browser support and current usage patterns, compare the snippet with MDN Web Docs before adopting it in production.

Hi Guys,

In this example,I will leran you how to get value of selected option in javascript.you can simply and easy to get value of selected option in javascript.

In JavaScript, selectedIndex property is used to set the value of a select box element. The selectedIndex property sets or returns the index of the selected value in a drop-down list.

I will you use the onclick and onchange to get value selected option.you can easy to get selected option value bellow follow example.

Example 1: Using onchange() event

<!DOCTYPE html> 
<html> 
<head>
    <title>How to Get Value of Selected Option In Javascript - nicesnippets.com</title>
</head>
<body> 
    <label>Select State :</label>  
    <select id="mySelect" onchange="myFunction();"> 
        <option>Gujarat</option> 
        <option>Goa</option> 
        <option>Keral</option> 
    </select> 
    <script> 
        function myFunction() { 
            var result = document.getElementById("mySelect").value; 
            alert(result);
        } 
    </script> 
</body> 
</html>  

Example 2: Using onclick() event

<!DOCTYPE html> 
<html> 
<head>
    <title>How to Get Value of Selected Option In Javascript - nicesnippets.com</title>
</head>  
<body> 
    <label>Select State :</label>
    <select id="mySelect"> 
        <option>Gujarat</option> 
        <option>Goa</option> 
        <option>Keral</option> 
    </select> 
    <button onclick="myFunction()"> 
      Get Value
    </button> 
    <script> 
        function myFunction() { 
            var result = document.getElementById("mySelect").value; 
            alert(result);
        } 
    </script> 
</body> 
</html>

It will help you....

# JavaScript