Building on Rick Viscomi's answer, try using the HTML Select Element's selectedOptions property:
let txtSelectedValuesObj = document.getElementById('txtSelectedValues');
[...txtSelectedValuesObj.selectedOptions].map(option => option.value);
In detail,
selectedOptions
returns a list of selected items....
is spread syntax. It expands the HTMLCollection
's elements.[...]
creates a mutable Array
object from these elements, giving you an array of HTMLOptionElements
.map()
replaces each HTMLObjectElement
in the array (here called option
) with its value (option.value
).Dense, but it seems to work.
Watch out, selectedOptions
isn't supported by IE!