<div>
<select name="food">
<option value="1">гамбургер</option>
<option value="2">чизбургер</option>
<option value="3">шитбургер</option>
</select>
</div>
<div>
<select name="beverage">
<option value="1">чай</option>
<option value="2">кофе</option>
<option value="3">потанцуем</option>
</select>
</div>
<button>Заменить меню</button>
div, button {
margin: 1em;
}
const replaceSelects = () => {
const selects = document.querySelectorAll('select');
const customSelect = document.createElement('select');
['яблоко','манго','банан'].forEach((c, i) => {
const option = document.createElement('option');
option.value = i;
option.innerText = c;
customSelect.appendChild(option);
});
Array.from(selects)
.forEach(el => {
el.parentNode.replaceChild(customSelect.cloneNode(true), el);
});
};
document.querySelector('button')
.addEventListener('click', replaceSelects);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.