let input = document.getElementById("input");
let menu = document.getElementById("menu");
let add = document.getElementById("addtask");
let inner = document.createElement("input");
let task = document.createElement("div");
let deleteBtn = document.createElement("button");
let btnDetails = document.createTextNode("Delete");
let content = document.createElement("p")
// HTML structure
task.appendChild(content);
task.appendChild(deleteBtn);
deleteBtn.appendChild(btnDetails)
// adding classes & ids
task.classList.add("task");
deleteBtn.id ="btn"
// events
deleteBtn.addEventListener('click', del) // the problem
add.onclick = function () {
if (input.value !== undefined || input.value.length !== 0) {
content.innerText = input.value;
let cloned = task.cloneNode(true);
if (input.value == ''){
} else {
menu.appendChild(cloned)
}
} else {
console.log("empty");
}
};
// the problem
function del() {
console.log("deleted")
I want to add event 'click' on the button created by the function (add.onclick) and addEventListener dosn't work... here is my pen : https://codepen.io/mahdtomar/pen/KKvEjOr i got it , thanks for everyone