2017/03/08

How to get the selected option in a drop down list

This post is about how to get the selected option in a drop down list. The source of the code is at https://www.w3schools.com/js/tryit.asp?filename=try_dom_select_option.

Code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html>
<head>
<script>
function getOption() {
    var obj = document.getElementById("mySelect");
    document.getElementById("demo").innerHTML = 
    obj.options[obj.selectedIndex].text;
}
</script>
</head>
<body>

<form>
Select your favorite fruit:
<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>
<br><br>
<input type="button" onclick="getOption()" value="Click Me!">
</form>

<p id="demo"></p>

</body>
</html>

Reference:

JavaScript HTML Input Examples
https://www.w3schools.com/js/js_input_examples.asp


No comments:

Post a Comment