Skip to content
the poor coder

selected option using javascript

Poor Man
Published
Reading time
2 min read
On this page

Selected Option using JavaScript#

Have you ever wanted to know which option a user has selected in a drop-down list using JavaScript? Well, you're in luck because it's actually quite easy to do.

First, let's set up our HTML. We'll create a select element with a few options:

Text
<div>
  <select id="mySelect">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
  </select>
</div>

Next, we'll create a button that will trigger the JavaScript function:

Text
<button onclick="getSelectedOption()">Get Selected Option</button>

Now, let's write the JavaScript function:

Text
<script>
  function getSelectedOption() {
    let select = document.getElementById("mySelect");
    let selectedOption = select.options[select.selectedIndex].value;
    console.log(selectedOption);
  }
</script>

Let's break this down:

Step 1: Get the select element#

We first need to get the select element using its ID. We do this using the document.getElementById() method:

Text
let select = document.getElementById("mySelect");

Step 2: Get the selected option#

Next, we need to get the selected option. We do this by accessing the options property of the select element and getting the selectedIndex:

Text
let selectedOption = select.options[select.selectedIndex].value;

This will give us the value of the selected option. If you want to get the text of the selected option instead, you can use select.options[select.selectedIndex].text.

Step 3: Do something with the selected option#

Finally, we can do something with the selected option. In this example, we're just logging it to the console:

Text
console.log(selectedOption);

And that's it! You now know how to get the selected option in a drop-down list using JavaScript.

There are other ways to accomplish this as well. One way is to add an event listener to the select element that triggers a function when the selection changes:

Text
select.addEventListener("change", function() {
  let selectedOption = select.options[select.selectedIndex].value;
  console.log(selectedOption);
});

This will trigger the function every time the selection changes.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC