How to Get Value of Selected Option In Javascript?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Today now in this example, I will show you how to get value of selected option by using javascript. So we can simply and easy to get any value of selected option by using  javascript.

So in JavaScript, selectedIndex property is used for set the value of a select box element. And also the selectedIndex property sets or it will returns the index of the selected value in a drop-down list.

So here I will use the onclick and onchange to get value selected option. So we can easy to get selected option value by using bellow follow example.

Example 1: Using onchange() event
<!DOCTYPE html> 
<html> 
<head>
    <title>How to Get Value of Selected Option In Javascript - Codingspoint.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 - Codingspoint.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>

Read Also : Laravel 8 Eloquent Global Scope Tutorial Example

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →