Skip to content

Commit a1074a4

Browse files
committed
fixes reinitialization
1 parent d7853db commit a1074a4

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ multiSelect.js is a simple, pure vanilla Javascript library that's progressively
4545
<option value="Peaches">Peaches</option>
4646
</select>
4747
```
48-
<!--
49-
```javascript
50-
// invoke multiSelect()
51-
multiSelect();
52-
``` -->
5348

5449
> It's essential that you name the class `multi` and set an initial option to `selected`, `disabled`, this will act as the placeholder value.
5550
@@ -63,6 +58,12 @@ console.log(vals); // "Banana,Apples,..."
6358
vals.split(','); // [Bananas, Apples, ...]
6459
```
6560

61+
### Reinitialization
62+
63+
* By default, multiSelect elements will be initialized on page ready. However if you're using an async script, you may add multis after the page load by invoking `multiSelect()` or `window.multiSelect.refresh()`. This will initialize any new `.multi` elements that have not yet been created.
64+
65+
Special thanks to Hirbod for bringing this Issue (#1) to my attention.
66+
6667
## Support
6768

6869
For general questions about autoComplete.js, tweet at [@kleimaj].

js/multiselect.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ const updateContainer = (container, inner, selected, opt) => {
1111
// console.log(container)
1212
container.dispatchEvent(new window.Event('change', { bubbles: true }));
1313
};
14-
const listItemEventListener = () => {};
14+
const buttonToggle = (display, dropdown) => {
15+
display.classList.toggle('multi__dropdown--toggle');
16+
dropdown.classList.toggle('multi--hidden');
17+
}
1518
const map = {};
1619
let multi_idx = 0;
1720
const multiSelect = () => {
1821
Array.from(document.querySelectorAll('.multi'))
1922
.filter(
20-
(multi) => multi.getAttribute('data-multiselect-initialized') === null,
23+
(multi) => {
24+
return multi.getAttribute('data-multiselect-initialized') === null
25+
}
2126
)
2227
.forEach((el) => {
23-
console.log(el);
2428
// stores values of dropdown
2529
map[multi_idx] = [];
2630
// array of options
@@ -34,6 +38,8 @@ const multiSelect = () => {
3438

3539
// Add data-multiselect-initialized attribute
3640
container.setAttribute('data-multiselect-initialized', true);
41+
container.setAttribute('key', multi_idx);
42+
const key = multi_idx;
3743
// carry over ids if exist
3844
// container.id = el.id;
3945
el.id ? (container.id = el.id) : '';
@@ -85,27 +91,28 @@ const multiSelect = () => {
8591
li.innerText = opt[i].innerText;
8692
li.addEventListener('click', (e) => {
8793
e.stopPropagation();
88-
if (map[multi_idx].includes(e.target.innerText)) {
89-
map[multi_idx].splice(
90-
map[multi_idx].indexOf(e.target.innerText),
94+
if (map[key].includes(e.target.innerText)) {
95+
map[key].splice(
96+
map[key].indexOf(e.target.innerText),
9197
1,
9298
);
9399
e.target.classList.remove('multi__li-item--selected');
94100
} else {
95-
map[multi_idx].push(e.target.innerText);
101+
map[key].push(e.target.innerText);
96102
e.target.classList.add('multi__li-item--selected');
97103
}
98104
// e.target.parentElement.removeChild(e.target);
99-
updateContainer(container, inner, map[multi_idx], opt);
105+
updateContainer(container, inner, map[key], opt);
100106
});
101107
// li.appendChild(a);
102108
list.appendChild(li);
103109
}
104110
// add event listener to container to show / hide dropdown
105111
container.addEventListener('click', (e) => {
106112
e.stopPropagation();
107-
display.classList.toggle('multi__dropdown--toggle');
108-
dropdown.classList.toggle('multi--hidden');
113+
buttonToggle(display, dropdown);
114+
// display.classList.toggle('multi__dropdown--toggle');
115+
// dropdown.classList.toggle('multi--hidden');
109116
});
110117
// append dropdown to container
111118
container.appendChild(dropdown);
@@ -136,4 +143,18 @@ window.multiSelect = {
136143
refresh() {
137144
multiSelect();
138145
},
146+
// addMulti() {
147+
// const root = document.querySelector('#root');
148+
// const select = document.createElement('select');
149+
// select.classList.add('multi');
150+
// const option = document.createElement('option');
151+
// option.selected = true;
152+
// option.disabled = true;
153+
// option.innerHTML="Default"
154+
// select.appendChild(option);
155+
// const option_two = document.createElement('option');
156+
// option_two.innerHTML="Banana";
157+
// select.appendChild(option_two);
158+
// root.appendChild(select);
159+
// }
139160
};

0 commit comments

Comments
 (0)