Select value Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Select

Description

The value property sets or gets the selected option in a drop-down list.

Set the value property with the following Values

Value Description
value Sets the value of an <option> element in a drop-down list that should get selected.

If the value does not exist, the drop-down list will display an empty option

Return Value

A String, representing the value of the value attribute of an <option> element in the drop-down list.

If the drop-down list allows multiple selections, the first selected option is returned.

If there is no selected options, nothing is returned.

The following code shows how to change the selected option to "banana":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form>
Select your favorite fruit:
<select id="mySelect">
  <option value="a">Apple</option>
  <option value="orange">Orange</option>
  <option value="pineapple">Pineapple</option>
  <option value="banana">Banana</option>
</select>/*  ww  w.j  a v  a2  s.c  o m*/
</form>

<button type="button" onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
    document.getElementById("mySelect").value = 'banana';

}
</script>

</body>
</html>

Related Tutorials