function getValueSelect(){ let selectValue = document.getElementById("selectID").value let request = new XMLHttpRequest(); let button = document.getElementById("buttonAdd"); let count = parseInt(button.dataset.count) + 1; button.dataset.count = count; console.log(count); request.open("GET", "/admin/set-value-select?value=" + selectValue + "&count=" + count, true) request.addEventListener("load", () => { let responseText = request.responseText; let targetElement = document.getElementById("buttonAdd"); let divElement = document.createElement("div"); divElement.innerHTML = responseText; targetElement.insertAdjacentElement("beforebegin", divElement); let deleteButton = document.getElementById("deleteButton"); deleteButton.addEventListener("click", () => { divElement.remove(); }); divElement.appendChild(deleteButton); }); request.send() } document.addEventListener("DOMContentLoaded", function() { let divElement = document.getElementsByClassName("form-block"); let deleteButton = document.getElementsByClassName("deleteButton"); console.log(deleteButton); for(let i = 0; i < deleteButton.length; i++){ deleteButton[i].addEventListener("click", () => { deleteButton[i].parentElement.remove(); }); } });