diff --git a/MultiSelect.js b/MultiSelect.js index 836f6de..5e696c9 100644 --- a/MultiSelect.js +++ b/MultiSelect.js @@ -5,8 +5,13 @@ * Released under the MIT license */ class MultiSelect { + static registry = new Map(); // Stores all instances by element ID constructor(element, options = {}) { + // Register this instance + if (element.id) { + MultiSelect.registry.set(element.id, this); + } let defaults = { placeholder: 'Select item(s)', max: null, @@ -256,5 +261,19 @@ class MultiSelect { return this.options.height; } + clearSelected() { + let selectAllButton = this.element.querySelector('.multi-select-all'); + if (!selectAllButton.classList.contains('multi-select-selected')) { + selectAllButton.classList.add('multi-select-selected'); + } + selectAllButton.click(); + } + + // Static helper to fetch instance anywhere + static getById(id) { + return MultiSelect.registry.get(id); + } + } document.querySelectorAll('[data-multi-select]').forEach(select => new MultiSelect(select)); + diff --git a/clear_demo.html b/clear_demo.html new file mode 100644 index 0000000..03ee3c2 --- /dev/null +++ b/clear_demo.html @@ -0,0 +1,133 @@ + + +
+ + +