Skip to content

Instantly share code, notes, and snippets.

@mgrubinger
Created February 13, 2019 14:26
Show Gist options
  • Select an option

  • Save mgrubinger/c7f293d16df2a3092fda855b092e536a to your computer and use it in GitHub Desktop.

Select an option

Save mgrubinger/c7f293d16df2a3092fda855b092e536a to your computer and use it in GitHub Desktop.

Revisions

  1. mgrubinger created this gist Feb 13, 2019.
    12 changes: 12 additions & 0 deletions selectbox-limit-max-selections.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    (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;
    });
    }
    }
    })();