HTML | DOM Option value Property
The Option value property in HTML DOM is used to set or return the value of the <option> element. The value is sent to the server when the form is submitted. This attribute contains the value text which sent to the server.
Syntax:
- It returns the option value property.
optionObject.value
- It is used to set the option value property.
optionObject.value = value
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Property Values: It contains single property value value. This attribute contains the value text which sent to the server.
Return Value: It returns a string value which represent the text value sent to the server.
Example 1: This example illustrate how to return the Option value property.
<!DOCTYPE html> < html > < head > < title > HTML DOM Option Value Property </ title > </ head > < body style = "text-align: center;" > < h1 style = "color: green;" > w3wiki </ h1 > < h2 >DOM Option value Property</ h2 > <!-- List of Option elements --> < select id = "GFG" > < option value = "merge" >Merge Sort</ option > < option value = "bubble" >Bubble Sort</ option > < option value = "insertion" >Insertion Sort</ option > < option value = "quick" >Quick Sort</ option > </ select > < br >< br > < button onclick = "myBeginner()" > Submit </ button > < p id = "sudo" style = "font-size:20px;color:green;" ></ p > <!-- Script to return selected option value --> < script > function myBeginner() { var ind = document.getElementById("GFG").selectedIndex; var opt = document.getElementsByTagName("option")[ind].value; document.getElementById("sudo").innerHTML = opt; } </ script > </ body > </ html > |
Output:
Before Click on the Button::
After Click on the Button:
Example 2: This example illustrates how to set the Option value property.
<!DOCTYPE html> < html > < head > < title > HTML DOM Option Value Property </ title > </ head > < body style = "text-align: center;" > < h1 style = "color: green;" > w3wiki </ h1 > < h2 >DOM Option value Property</ h2 > <!-- List of Options --> < select id = "GFG" > < option value = "merge" >Merge Sort</ option > < option value = "bubble" >Bubble Sort</ option > < option value = "insertion" >Insertion Sort</ option > < option value = "quick" >Quick Sort</ option > </ select > < br >< br > < button onclick = "myBeginner()" > Submit </ button > < p id = "sudo" style = "font-size:20px;color:green;" ></ p > <!-- script to set Option value property --> < script > function myBeginner() { document.getElementById("GFG").value = "Heap"; document.getElementById("sudo").innerHTML = "The value of the value attribute has " + "been changed from 'merge' to 'Heap' "; } </ script > </ body > </ html > |
Output:
Before Click on the Button:
After Click on the Button:
Supported Browsers: The browser supported by DOM Option value property are listed below: