How to Get Value of Selected Option In Javascript?

Admin   HTML5 - CSS   400  2021-03-13 03:40:04

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 - </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 - </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....