Hàm removeChild

Mọi người cho em hỏi đoạn removeChild ở phía dưới nếu em thay i ở childNodes bằng một chỉ số bất kỳ thì tại vị trí phần tử ấy sẽ xóa được còn em chỉ giữ nguyên i để vòng for thêm phần tử thì lại bị lỗi Uncaught TypeError: Failed to execute 'removeChild' on 'Node': parameter 1 is not of type 'Node'. là sao ạ? Em mong mọi người giải thích giúp em và đề xuất cho em một cách giải quyết với ạ.

<!DOCTYPE html>
<html>
<head>
    <title>To do list</title>
    <meta charset="utf-8"/>
    <style>
        
    </style>


</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>


</body>

<script> 
    
    var bodyTag;
    var divArray;
    function inputClear(){ 
        document.getElementById('textbox').value = "";
    }
    function eventOfButton(inputClear){
        var textBValue = document.getElementById('textbox').value;
        
        var divTotal = document.createElement('DIV');
        divTotal.className = "Total";
        var listDv = document.getElementsByClassName('Total');
        divArray = listDv;
        var divShow = document.createElement('DIV');
        var showContent = document.createTextNode(textBValue);
        divShow.appendChild(showContent);

        var closeDiv = document.createElement('BUTTON');
        closeDiv.className = 'dlt';
        var listClsDv = document.getElementsByClassName('dlt');
        
        var closeBtn = document.createTextNode("Xóa");
        closeDiv.appendChild(closeBtn);

        divTotal.appendChild(divShow);
        divTotal.appendChild(closeDiv);
        var divad = document.getElementById('ad');
        bodyTag = divad;
        divad.appendChild(divTotal); 
        inputClear();

        for(var i = 0 ; i < listClsDv.length ; i++){
            listClsDv[i].onclick = function (){
                bodyTag.removeChild(bodyTag.childNodes[i]);
            }
        }
    }

  
</script>

</html>

Đây lỗi ở đây là bạn lấy sai vì nút button xóa là con của bạn nó là con của DIV class “Total”. Nên ở đây ta sẽ gọi chính nó và kêu về parent và parent tự hủy !

https://jsfiddle.net/leotwot/w9068f7s/13/

// bodyTag.removeChild(bodyTag.childNodes[i]);
this.parentElement.remove();

Kiểu hàm remove của bạn hay ghê mà mình chưa thấy trên w3schools. Với cả trường hợp của mình bạn có thể chỉnh lại được không để mình hiểu rõ hơn?

Code của bạn không hiểu chỗ nào?

ý em là anh thử xóa qua các class get được trong code của em rồi anh cho em xem mẫu được không ạ?

ý a vẫn chưa hiểu em muốn biết gì?

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?