How to get select option value in JavaScript

There are many methods to get option value in javascript but in my view the simplest way to do that is to make function in javascript .

Following are the two methods are given follow any one of them to get the value of option.

<select  onchange=” myFunction()“>

<option value=”1”>One</option>

<option value=”2”>Two</option>

<option value=”3”>Three</option>

<option value=”4”>Four</option>

<option value=”5”>Five </option>

</sclect>

Method 1:-

<script>

function myFunction() {

 var x = document.getElementById(“customer_s”).selectedIndex;

alert(document.getElementsByTagName(“option”)[x].value);

}

</script>

Method 2:-

<script>

function myFunction() {

 var x = document.getElementById(“customer_s”).selectedIndex;

 var y= document.getElementsByTagName(“option”)[x].value;

window.location.href = “http://www.yoursite.com?user=” + y;

}

</script>

Leave a comment