-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropdown.js
31 lines (29 loc) · 1.14 KB
/
dropdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var lst = document.querySelectorAll('[dropdown]');
lst.forEach(dropdown => {
createDropdown(dropdown); // actually sets up the dropdown's inner HTML
tweakDropdown(dropdown); // tweaks the dropdown to fix some bugs
});
function createDropdown(dropdown) {
dropdown.classList.add('dropdown');
dropdown.innerHTML = '<button id="ddtext" class="dropbtn">Dropdown</button>\n'
+ '<div class="dropdown-content">\n'
+ dropdown.innerText
.split(' ')
.map(x => `<button value="${x}">${x}</button><br>\n`)
.join('')
+ '</div>';
}
function tweakDropdown(dropdown) {
var text = dropdown.querySelector('#ddtext');
var options = Array.from(dropdown.children[1].querySelectorAll('button'));
options.forEach(op => {
op.addEventListener('click', (e) => {
dropdown.value = op.value;
text.innerText = op.innerText + '▼';
options.forEach(x => x.style.backgroundColor = null);
op.style.backgroundColor = '#4A4A52';
dropdown.dispatchEvent(new Event('change'));
});
});
options[0].dispatchEvent(new Event('click'));
}