Mọi người giúp mình gắn sự kiện onclick vào listClsDv
trong vòng for giúp mình để mình có thể click nút xóa chứ mình thử mọi kiểu gắn onclick từ addeventlistener hay .onclick rồi mà nút xóa vẫn không thể gắn được onlick
<!DOCTYPE html>
<html>
<head>
<title>To do list</title>
<meta charset="utf-8"/>
<style>
</style>
<script>
var i;
var cloneListClsDv;
function onclickClose(){
cloneListClsDv.removeChild(cloneListClsDv.childNodes[i]);
}
function inputClear(){
document.getElementById('textbox').value = "";
}
function eventOfButton(inputClear){
var textBValue = document.getElementById('textbox').value;
var divTotal = document.createElement('DIV');
divTotal.id = "Total";
var divShow = document.createElement('DIV');
var showContent = document.createTextNode(textBValue);
divShow.appendChild(showContent);
var closeDiv = document.createElement('BUTTON');
closeDiv.id = 'dlt';
var listClsDv = document.getElementById('dlt')
cloneListClsDv = listClsDv;
var closeBtn = document.createTextNode("Xóa")
closeDiv.appendChild(closeBtn);
divTotal.appendChild(divShow);
divTotal.appendChild(closeDiv);
document.getElementById('ad').appendChild(divTotal);
inputClear();
for(i = 0 ; i < divTotal.length ; i++){
listClsDv[i].addEventListener("click", onclickClose);
}
}
</script>
</head>
<body>
<input type="text" id="textbox" placeholder="Xin mời nhập">
<button id="btn" onclick="eventOfButton(inputClear)">Thêm</button>
<div id="ad"></div>
<h2>JavaScript addEventListener()</h2>
<p>This example uses the addEventListener() method to attach a click event to a button.</p>
<button id="myBtn">Try it</button>
</body>
</html>