get value multiple select option javascript code example

Example 1: get array of selected options from select element

var values = $('#select-meal-type').val();

Example 2: how to get values from select multiple in js

document.getElementById('submit').onclick = function() {
  var selected = [];
  for (var option of document.getElementById('pets').options) {
    if (option.selected) {
      selected.push(option.value);
    }
  }
  alert(selected);
}

Example 3: get array of selected options from select element

function getSelectValues(select) {
  var result = [];
  var options = select && select.options;
  var opt;

  for (var i=0, iLen=options.length; i<iLen; i++) {
    opt = options[i];

    if (opt.selected) {
      result.push(opt.value || opt.text);
    }
  }
  return result;
}

Tags:

Misc Example