How to Get Value of Selected Option In Javascript?

JavaScript

Nicesnippets

1339

24-01-2020


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


Recommended Posts