Skip to content

Instantly share code, notes, and snippets.

@marcusshepp
Created September 22, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusshepp/09caf5e03a254c488592b96ffc4e65f0 to your computer and use it in GitHub Desktop.
Save marcusshepp/09caf5e03a254c488592b96ffc4e65f0 to your computer and use it in GitHub Desktop.
Get selected values from a select element with multiple = 'multiple'
function get_selected_values(select_element){
/*
Vanilla JS way of returning an array of selected elements from a
select element with multiple='mulitple'.
Used to allow for multiple Evaluation Categories to be
selected.
*/
var result = [];
var options = select_element && select_element.options;
var opt;
for (var i = 0, i_len = options.length; i < i_len; i++){
opt = options[i];
if (opt.selected){
result.push(opt.value || opt.text);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment