I have this code , where the 'innerHTML' lines is removing the onClick function from button "one" and "two" . If i disable that line, the function works for all three buttons. That 'innerHTML' in this case is doing nothing but adding empty string. I need to add something later here. But for now it's just didn't work even when it's empty string.
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body class="body">
<div id="buttoncontainer"></div>
<script>
function test() {
alert("click");
}
values = ["one", "two", "three"];
const buttoncontainer = document.getElementById("buttoncontainer");
for (const val of values) {
var button = document.createElement("button");
button.id = "button_" + val;
button.classList.add("id_button");
button.addEventListener("click", test);
button.textContent = val;
buttoncontainer.innerHTML += ""; // <--- COMMENT THIS LINE TO FIX -----
buttoncontainer.appendChild(button);
}
</script>
</body>
</html>