The code below always shows opt 5
instead of the one I clicked.
In HTML :
<div id = "dropdown_1" class="dropdown">
<button onclick="dropdown_toggle()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
</div>
</div>
In another js
file :
var select = document.getElementById("myDropdown");
var options = ["opt 1", "opt 2", "opt 3", "opt 4", "opt 5"];
for(var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("a");
el.textContent = opt;
el.value = opt;
el.onclick = function(event) {
console.log(opt);
}
select.appendChild(el);
}