how to get multiple selected values of select box in javascript code example

Example 1: 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 2: javascript select multiple values

const selected = document.querySelectorAll('#select-meal-type option:checked');
const values = Array.from(selected).map(el => el.value);

Tags:

Html Example