How to Get Selected Value in Dropdown List using JavaScript?
Last Updated :
08 Oct, 2024
A dropdown list is an HTML element that allows users to select one option from a list of options. The <select> and <option> tags are used to create dropdown lists in HTML. In this article, we will see how to get selected value from dropdown list using JavaScript.
Basic HTML Structure for a Dropdown
To create a dropdown list, you will use the following HTML structure:
<select id="dropdown-id">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
</select>
This syntax create a dropdown list with three options HTML, CSS, and JavaScript. Each options has an individual attribute (for example - value="html").
To create a dropdown list, You can refer this article How to create the dropdown menu?.
Get the Selected Value from Dropdown using DOM value Property
To get the selected value from dropdown list, we need to use JavaScript. The HTML DOM value property is used to get the selected value of a dropdown list item.
Syntax
selectedElement.value;
Example: The below code uses the value property to get the value of the selected dropdown item.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Get Selected Value in Dropdown
List using JavaScript
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>
<b>How to get selected value in dropdown
list using JavaScript?</b>
</p>
<label>Select one from the given options:</label>
<select id="select1">
<option value="free">Free</option>
<option value="basic">Basic</option>
<option value="premium">Premium</option>
</select>
<p>
The value of the option selected is:
<span class="output"></span>
</p>
<button onclick="getOption()">
Check option
</button>
<script>
function getOption() {
selectElement =
document.querySelector('#select1');
output = selectElement.value;
document.querySelector('.output').textContent = output;
}
</script>
</body>
</html>
Output
value propertyGet the Selected Value from Dropdown using DOM selectedIndex Property
The selectedIndex property gives you the index (position) of the currently selected option in the dropdown list. This index starts at 0. If no option is selected, it returns -1. You can then use this index to find the value of the selected option.
Syntax
SelectElement.options[selectedElement.selectedIndex].value;
Example: The eblow example implements the selectedIndex property to get the value of selected dropdown item.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Get Selected Value in Dropdown
List using JavaScript
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<p>
<b>How to get selected value in dropdown
list using JavaScript?</b>
</p>
<label>Select one from the given options:</label>
<select id="select1">
<option value="free">Free</option>
<option value="basic">Basic</option>
<option value="premium">Premium</option>
</select>
<p>
The value of the option selected is:
<span class="output"></span>
</p>
<button onclick="getOption()">
Check option
</button>
<script>
function getOption() {
selectElement =
document.querySelector('#select1');
output =
selectElement.options
[selectElement.selectedIndex].value;
document.querySelector('.output').textContent = output;
}
</script>
</body>
</html>
Output
SelectedIndex property with option property
Similar Reads
How to Create a Dropdown List with Array Values using JavaScript? To add elements to the select element from the JavaScript array, We can also get selected values in the dropdown list using JavaScript. Here we will populate the dropdown list with an array.Below are the methods to create a dropdown list with array values:Table of ContentUsing Pure JavaScriptUsing j
2 min read
How to get the text of option tag by value using JavaScript ? To get the text of the option tag by value using JavaScript, we have multiple approaches. we are going to learn how to get the text of the option tag by value using JavaScript. These are the following approaches that are discussed below: Table of Content Using value PropertyUsing Object.values() Met
3 min read
How to get selected option from Dropdown in jQuery ? In this article, we learn how to get the selected option from the dropdown list. This article requires some familiarity with HTML, CSS, JavaScript, and jQuery. We can solve this challenge by using the jQuery val() method. The val() method in jQuery is used to set or return the value of an attribute
2 min read
How to change Selected Value of a Drop-Down List using jQuery? We will change the default selected value of the dropdown using jQuery. In an HTML select tag, the default selected value is the value given to the first option tag. With jQuery, it is easy to change the selected value from a drop-down list. Below are the methods to change the selected value of a dr
3 min read
Get the Value of Text Input Field using JavaScript Getting the value of a text input field using JavaScript refers to accessing the current content entered by a user in an input element, such as <input> or <textarea>, by utilizing the value property. This allows for dynamic interaction and data manipulation in web forms.Syntax inputField
2 min read