This post will discuss how to get the selected values in a multi-select dropdown in plain JavaScript.

We can select the multiple options in the dropdown list using the multiple attribute. There are several ways in JavaScript to return the selected values in a multi-select dropdown.

1. Using for…of statement

The idea is to iterate over all dropdown options using the for…of statement and collect values of all the <option> elements having the selected attribute.

In JavaScript, the options property can be used to get the list of <option> elements contained within the <select> element.

JS


HTML



Edit in JSFiddle

2. Using filter() with map() function

The idea is to get an array of the all <option> elements contained within the <select> element and filter the selected ones. Then, we map the options into their respective values using the map() function.

JS


HTML



Edit in JSFiddle

3. Using selectedOptions property

Instead of getting the list of all <option> elements, we can use the property selectedOptions to get the list of only selected <option> elements.

The following code gets the array of selected options using selectedOptions with the spread syntax (or Array.from()), and then map the <option> elements into their respective values.

JS


HTML



Edit in JSFiddle

4. Using querySelectorAll() method

Another plausible way is to use the :checked pseudo-class selector, which can match with any checked option in a <select> element. When used with the querySelectorAll() method, it returns the list of all checked options.

JS


HTML



Edit in JSFiddle

That’s all about getting selected values in a multi-select drop-down with JavaScript.