Skip to content

Instantly share code, notes, and snippets.

@mgrubinger
Created February 13, 2019 14:26
Show Gist options
  • Save mgrubinger/c7f293d16df2a3092fda855b092e536a to your computer and use it in GitHub Desktop.
Save mgrubinger/c7f293d16df2a3092fda855b092e536a to your computer and use it in GitHub Desktop.
Limit the maximum of currently selected options for a selectbox
(function() {
var verified = [];
document.querySelector('#myselector').onchange = function(e) {
if (this.querySelectorAll('option:checked').length <= 3) {
verified = Array.apply(null, this.querySelectorAll('option:checked'));
} else {
Array.apply(null, this.querySelectorAll('option')).forEach(function(e) {
e.selected = verified.indexOf(e) > -1;
});
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment